First commit software for Spejblarm bord
[LPC2xxx_and_RobotSpejbl.git] / app / spejbl_rakovm1 / main.c
blob876804dc3fc56dc3677a3b0a3998873630ac62cf
1 /* Autor: Martin Rakovec */
2 #include <lpc21xx.h> /* LPC21xx definitions */
3 #include <deb_led.h>
4 #include <can/canmsg.h>
5 #include <can/lpcan.h>
6 #include <can/lpcan_vca.h>
8 vca_handle_t can;
9 uint8_t sendcount = 0;
11 /** ADC initialization
12 * This function initialize AD convertor
14 * @param clk_div clock divider
17 void adc_init(unsigned clk_div) {
18 /* Set AD control registry */
19 ADCR = ADC_CR_ADC0_m | ((clk_div&0xff)<<8)/*ADC clock*/ | ADC_CR_PDN_ON_m;
20 ADCR |= (1<<26); /* conversion started on falling edge of MAT0.1 */
23 void timer_irq() __attribute__((interrupt));
25 /** Timer inicialization
26 * This function initializace timer0
28 * @param prescaler prescaler
29 * @param period period
32 void timer_init(uint32_t prescale, uint32_t period){
33 T0PR = prescale - 1; // set prescaler
34 T0MR1 = period - 1; // set period (match1)
35 T0MCR = 0x3<<3; //interrupt and counter reset on match1
36 T0EMR = 0x1<<6 | 0x2; //set MAT0.1 low on match an hight now
37 T0CCR = 0x0; // no capture
38 T0TCR |=0x1; // start timer
41 /** Timer interrupt initialization
42 * Set VIC
43 * @param irq_vect vector number for timer interrupt
45 void timer_init_irq(unsigned irq_vect){
46 /* Set interrupt vector*/
47 ((uint32_t*)&VICVectAddr0)[irq_vect] = (uint32_t)timer_irq;
48 ((uint32_t*)&VICVectCntl0)[irq_vect] = 0x20 | 4;
49 /* enable timer interrupt */
50 VICIntEnable = 0x00000010;
53 uint16_t value;
54 /** Timer interrupt control
56 void timer_irq(){
58 canmsg_t message;
59 message.flags = 0;
60 message.id = 0x400;
61 /* wait for end of conversion */
62 while((ADDR & (1<<31)) == 0);
63 T0EMR |= 0x2; //set MAT0.1 hight
64 /* Read value from AIN0*/
65 value = ((ADDR>>6)&0x3ff);
66 if (sendcount <10){
67 message.length = 1;
68 message.data[0] = value;
69 vca_send_msg_seq(can,&message,1);
70 sendcount++;
72 /* Clear interrupt flag */
73 T0IR = 0xffffffff;
74 VICVectAddr = 0;
77 void dummy_wait()
79 unsigned int wait = 7000000;
80 while(--wait);
83 int main (void) {
85 //MEMMAP = 0x1; //flash
86 MEMMAP = 0x2; //ram
88 VICIntEnClr = 0xFFFFFFFF;
89 VICIntSelect = 0x00000000;
91 /* CAN bus setup */
92 uint32_t btr;
93 lpcan_btr(&btr, 1000000 /*Bd*/, 20000000, 0/*SJW*/, 70/*%sampl.pt.*/, 0/*SAM*/);
94 lpc_vca_open_handle(&can, 0/*device*/, 0/*flags*/, btr, 10, 11, 12);
96 adc_init(3);
97 timer_init(10,1000);
98 timer_init_irq(13);
101 canmsg_t message;
102 message.flags = 0;
103 message.id = 0x401;
105 unsigned i = 0;
107 while(1)
109 i++;
110 message.length = 1;
111 message.data[0] = i;
112 deb_led_change(LEDG);
113 dummy_wait();
114 vca_send_msg_seq(can,&message,1);
115 sendcount=0;