#include <uart.h>
#include <engine.h>
#include <powswitch.h>
+#include <system_def.h>
+#include <servo.h>
-#define SPEED 30
+#define CAN_SPEED 1000000
+#define CAN_ISR 0
+
+#define ENG_ISR 1
+
+#define SERVO_ISR 5
+
+
+
+#define SPEED 50
+#define LOWSPEED 10
+
+static void Init_timer(void);
+void delay(int interval);
+void gotoPos(unsigned int position);
void init_perip(void) // inicializace periferii mikroprocesoru
init_engine_A();
//init_engine_B();
+
+ // init timer 0
+ Init_timer();
init_uart0((int)9600 ,UART_BITS_8, UART_STOP_BIT_1, UART_PARIT_OFF, 0 );
while(--i);
}
-
-void main ()
+volatile int delay_done=0 ;
+int main (void)
{
init_perip();
-
char text[6]= {'i','n','i','t',0x0D,0x0A};
int n;
{
uart0SendCh(text[n]);
}
+ unsigned int next_pos=0;
+ while(1)
+ {
+ next_pos= (unsigned int) uart0GetCh();
+ next_pos= next_pos -48; // recived character is in ASCII
+ gotoPos(next_pos);
+ }
+
+ char byeText[5]= {'b','y','e',0x0D,0x0A};
+ for(n=0;n<6;n++)
+ {
+ uart0SendCh(byeText[n]);
+ }
+}
- engine_A_dir(ENGINE_DIR_FW); //FW);
- //engine_B_dir(ENGINE_DIR_FW);
- engine_A_en(ENGINE_EN_ON);
- //engine_B_en(ENGINE_EN_ON);
- engine_A_pwm(SPEED);
- //engine_B_pwm(70);
+void timer0_isr(void) __attribute__ ((interrupt));
- unsigned char input; // data read from the carusel position sensor
- unsigned char decode[5]={1,4,5,2,3};
- unsigned int position= 0; // position of the carusel
- unsigned int readings=0; //how many times i read the same code
- unsigned char recChar;
- while(1)
+int i=0 ;
+
+void timer0_isr(void)
+{
+
+ if (i==0)
+ {
+ deb_led_off(LEDB);
+ i=1;
+ }
+ else
+ {
+ deb_led_on(LEDB);
+ i=0;
+ }
+
+ delay_done= 1;
+
+ // stop timer 0
+ //T0TCR = 0x00;
+
+ T0IR |= 0x00000001; // Clear match0 interrupt
+ VICVectAddr = 0x00000000;
+}
+
+/**
+ * initializes timer T0 to raise an interrupt and to reset T0counter when it matches T0MatchRegister0
+ * @param
+ */
+static void Init_timer(void)
{
+ VICVectAddr0 = (unsigned)timer0_isr; /* Set the ISR vector address vector0 = highest priority */
+ VICVectCntl0 = 0x20|4; /* Enable this vector and assign Timer IRQ to it */
+ VICIntEnable = 0x00000010; /* Enable the interrupt of timer0*/
+
+ T0MR0= 0x0003E8; // interrupt interval each 1s
+ T0PR = CPU_APB_HZ/1000; // Load prescaler for 1 Msec tick
+
+ T0MCR = 0x07; //resets timer0, stop timer 0 and generates interrupt when timer0 counter match MR0 register
+ T0TCR = 0x02; //Reset timer 0
+
+}
- deb_led_on(LEDG);
- deb_led_off(LEDY);
+/**
+ * set the carusel to the postion specified in position 0-3
+ */
+void gotoPos(unsigned int position)
+{
+ engine_A_dir(ENGINE_DIR_FW); //FW);
+ engine_A_en(ENGINE_EN_ON);
+
+ // at fist rotate fast, than slow down to get exactly to the right positin
+ engine_A_pwm(SPEED);
+ delay(350);
+ engine_A_pwm(LOWSPEED);
+
+ unsigned char decode[5]={1,4,5,2,3};
+ unsigned int readings=0; //how many times i read the same code
+ unsigned int input;
+
+ //if (position>4) position= 4; // reduce error states
- //dummy_wait();
- //input= ((IOPIN1>>16) & 0x07);
- //if (input>0)
- //{
- //uart0SendCh(input+48);
- //uart0SendCh(';');
- //dummy_wait();
-
- input= ((IOPIN1>>16) & 0x07);
- if (input==decode[position])
+ int not_done= 1;
+ while(not_done)
+ {
+ input= ((IOPIN1>>16) & 0x07);
+ if (input==decode[position])
+ {
+ readings= readings +1;
+ if (readings>2)
{
- readings= readings +1;
- if (readings>2)
- {
- //engine_A_en(ENGINE_EN_OFF);
- engine_A_pwm(0);
-
- uart0SendCh(position+49);
- uart0SendCh(';');
-
- position= position +1;
- if (position>4) position= 0;
- readings= 0;
-
- // wait for key press
- recChar =uart0GetCh();
- //engine_A_en(ENGINE_EN_ON);
- engine_A_pwm(SPEED);
- //dummy_wait();
- }
+ //stop motor
+ engine_A_pwm(0);
+
+ uart0SendCh(position+48);
+ uart0SendCh(';');
+ not_done= 0;
}
- //}
-
- deb_led_off(LEDG);
- deb_led_on(LEDY);
-
- //dummy_wait();
+ }
}
+ engine_A_en(ENGINE_EN_OFF);
+}
- char byeText[5]= {'b','y','e',0x0D,0x0A};
- for(n=0;n<6;n++)
- {
- uart0SendCh(byeText[n]);
- }
+/**
+ * wait time specified in the interaval
+ * int interval - interval in ms
+ */
+void delay(int interval)
+{
+ T0MR0= interval; // interrupt interval each 1s
+ T0TCR = 0x02; //Reset timer 0 and prescaler
+
+ delay_done= 0; // delay flag will be set by interrupt
+ T0TCR = 0x01; //Run timer 0
+
+ // wait for timer0 interrupt
+ while(delay_done==0){};
}