Hallo!
Für meine Arbeit muss ich einen AT90CAN64 programmieren. Habe leider
damit keinerlei Erfahrung...
Das Programm soll den MAX1148 ADC auslesen. Da dieser nur 8 Kanäle hat,
ich aber insgesamt 64 auslesen möchte, werden zudem über Port A
Multiplexer gesteuert. Ich benutze die von Atmel für diesen MC zur
Verfügung gestellt CAN software Libraries
([[http://www.atmel.com/dyn/products/product_card.asp?PN=AT90CAN64]])
Mein Problem ist, dass ich mit dem Scope keinerlei Signale am SPI sehe.
Hier mein Code
1 | //_____ I N C L U D E S ________________________________________________________
|
2 | #include <avr/io.h>
|
3 | #include <avr/interrupt.h>
|
4 | #include <avr/iocan64.h>
|
5 | #include "compiler.h"
|
6 | #include "at90can_drv.h"
|
7 | #include "spi_lib.h"
|
8 | #include "can_lib.h"
|
9 |
|
10 | //_____ D E F I N I T I O N S __________________________________________________
|
11 | // -------------- MCU LIB CONFIGURATION
|
12 | #define FOSC 8000 // 8.000 MHz External cristal
|
13 | #define F_CPU (FOSC*1000) // Need for AVR GCC
|
14 |
|
15 | // -------------- CAN LIB CONFIGURATION
|
16 | #define CAN_BAUDRATE 250 // in kBit
|
17 |
|
18 | // -------------- MISCELLANEOUS
|
19 | #define ADCSTART 0x8e // c.f. MAX1146-1149 datasheet
|
20 | #define ADCSSTRB PINB4
|
21 | #define NUM_CHANNELS 64
|
22 | #define NUM_DATA (NUM_CHANNELS*2)
|
23 |
|
24 | volatile U8 channelSelect[8] = {0,4,1,5,2,6,3,7}; // c.f. MAX1146-1149 datasheet
|
25 |
|
26 | U8 init(void)
|
27 | {
|
28 | // --- enable interrupts
|
29 | Enable_interrupt();
|
30 |
|
31 | // --- init SPI; c.f. "spi_lib.c"
|
32 | U8 spi_config = (SPI_MASTER|SPI_MSB_FIRST|SPI_DATA_MODE_0|SPI_CLKIO_BY_64);
|
33 | if( spi_init(spi_config) ){
|
34 | Spi_init_ss();
|
35 | Spi_disable_ss();
|
36 | }
|
37 | else return (0);
|
38 |
|
39 | // --- configure port A
|
40 | DDRA = (1<<DDA2)|(1<<DDA1)|(1<<DDA0);
|
41 | PORTA = 0x00;
|
42 |
|
43 | return (1);
|
44 | }
|
45 |
|
46 | void readAdc(unsigned int channel, U8 *result)
|
47 | {
|
48 | U8 buffer[3];
|
49 |
|
50 | buffer[0] = ADCSTART | (channelSelect[channel%8] << 4);
|
51 | buffer[1] = 0; buffer[2] = 0;
|
52 |
|
53 | Spi_enable_ss();
|
54 |
|
55 | // --- Send control byte
|
56 | spi_transmit_master(buffer[0]);
|
57 |
|
58 | // --- Wait for conversion complete
|
59 | while(ADCSSTRB == 0);
|
60 |
|
61 | buffer[1] = spi_getchar();
|
62 | buffer[2] = spi_getchar();
|
63 |
|
64 | Spi_disable_ss();
|
65 |
|
66 | result[2*channel] = buffer[1];
|
67 | result[2*channel+1] = buffer[2];
|
68 |
|
69 | return;
|
70 | }
|
71 |
|
72 | int main(void)
|
73 | {
|
74 | U8 meanValues[NUM_DATA];
|
75 | int channel;
|
76 |
|
77 | if(init()==0) return (0);
|
78 |
|
79 | while(1){
|
80 | for(channel=0; channel<NUM_CHANNELS; channel++){
|
81 | int value = channel/8;
|
82 | if((value != PORTA) && (value <= 7))
|
83 | PORTA = value;
|
84 | readAdc(channel,meanValues);
|
85 | } // for channel
|
86 |
|
87 | } // while(1)
|
88 |
|
89 | }
|
Hat jemand eine Idee, woran es liegen könnte? Wenn ich die 3 Zeilen, in
der ich Daten auf dem SPI hin und her schicke, auskommentiere, läuft
alles so wie es soll...
Gruss
Florian