1 /* Autor: Martin Rakovec */
2 #include <lpc21xx.h> /* LPC21xx definitions */
4 #include <can/canmsg.h>
6 #include <can/lpcan_vca.h>
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
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;
54 /** Timer interrupt control
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);
68 message
.data
[0] = value
;
69 vca_send_msg_seq(can
,&message
,1);
72 /* Clear interrupt flag */
79 unsigned int wait
= 7000000;
85 //MEMMAP = 0x1; //flash
88 VICIntEnClr
= 0xFFFFFFFF;
89 VICIntSelect
= 0x00000000;
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);
112 deb_led_change(LEDG
);
114 vca_send_msg_seq(can
,&message
,1);