code:interrupt_driven_ubx
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
code:interrupt_driven_ubx [2009/05/01 00:07] – created laurenceb | code:interrupt_driven_ubx [2009/05/11 20:17] (current) – laurenceb | ||
---|---|---|---|
Line 1: | Line 1: | ||
under construction... | under construction... | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== demo code ===== | ||
<code c> | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #include " | ||
+ | |||
+ | volatile ubx_gps_type Gps; | ||
- | static u08 state,lenght, | + | int uart_putchar(char c, FILE *stream) |
- | static gps_type gps; | + | |
- | switch(state) | + | |
{ | { | ||
- | case 0: | + | if (c == ' |
- | if(c==SYNC_1) | + | uart_putchar(' |
- | state=1; | + | loop_until_bit_is_set(UCSR0A, |
- | else | + | UDR0 = c; |
- | state=0; | + | return 0; |
- | break; | + | } |
- | case 1: | + | static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, |
- | if(c==SYNC_2) | + | |
- | state=2; | + | |
- | else | + | void main(void) |
- | state=0; | + | { |
- | break; | + | UCSR0B |
- | case 2: | + | UCSR0C |
- | class=c; | + | UBRR0L |
- | state=3; | + | UBRR0H |
- | break; | + | DDRD|=0x20; //use portd 5 as an output for led |
- | case 3: //then the id | + | stdout |
- | id=c; | + | sei(); //enable interrupts |
- | state=4; | + | TOGGLE_PIN; |
- | break; | + | for(;;) |
- | case 4: //the least significant byte of the lenght | + | { |
- | lenght=c; | + | if(Gps.packetflag==REQUIRED_DATA) |
- | state=5; | + | |
- | break; | + | |
- | case 5: //the most significant byte | + | |
- | lenght|=c<<8; | + | |
- | break; | + | |
- | case 6: //the data follows | + | |
- | lenght--; | + | |
- | if(!lenght) //we have reached the end of the data | + | |
- | state=7; | + | |
- | if(class==NAV) //we have nav data | + | |
{ | { | ||
- | if(id==LLH) //needs macros defining in the header file for the correct | + | printf(" |
+ | Gps.packetflag=0; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ISR(USART_RX_vect) // | ||
+ | { | ||
+ | static u08 state, | ||
+ | static u16 lenght; | ||
+ | static ubx_gps_type gps; | ||
+ | u08 c = UDR0; | ||
+ | switch(state) | ||
+ | { | ||
+ | case 0: | ||
+ | if(c==SYNC_1) | ||
+ | state=1; | ||
+ | else | ||
+ | state=0; | ||
+ | break; | ||
+ | case 1: //followed by the second one | ||
+ | if(c==SYNC_2) | ||
+ | state=2; | ||
+ | else | ||
+ | state=0; | ||
+ | break; | ||
+ | case 2: | ||
+ | class=c; | ||
+ | state=3; | ||
+ | break; | ||
+ | case 3: | ||
+ | id=c; | ||
+ | state=4; | ||
+ | break; | ||
+ | case 4: //the least significant byte of the lenght | ||
+ | lenght=c; | ||
+ | state=5; | ||
+ | break; | ||
+ | case 5: //the most significant byte | ||
+ | lenght|=c<< | ||
+ | counter=0; | ||
+ | state=6; | ||
+ | break; | ||
+ | case 6: //the data follows | ||
+ | if(class==NAV_CLASS) // | ||
{ | { | ||
- | if(lenght< | + | if(id==LLH_DATA)// |
- | gps[lenght+POS_OFFSET]=c; | + | { |
+ | if(lenght< | ||
+ | ((u08*)& | ||
+ | } | ||
+ | if(id==VELNED_DATA) | ||
+ | { | ||
+ | if(lenght< | ||
+ | ((u08*)& | ||
+ | } | ||
+ | if(id==SOL_DATA && lenght==SOL_POS) | ||
+ | gps.status=c; | ||
+ | if(id==SOL_DATA && lenght==SATS_POS) | ||
+ | gps.nosats=c; | ||
+ | / | ||
+ | { | ||
+ | counter++;// | ||
+ | if(counter==SATS_POS) | ||
+ | gps.nosats=c; | ||
+ | }*/ | ||
} | } | ||
- | if(id==VELNED) | + | lenght--; |
+ | if(!lenght) // | ||
+ | state=7; | ||
+ | break; | ||
+ | case 7: | ||
+ | if(checksum_1==c) | ||
+ | state=8; | ||
+ | else | ||
+ | state=0; | ||
+ | break; | ||
+ | case 8: | ||
+ | if(checksum_2==c) | ||
{ | { | ||
- | if(lenght< | + | if(class==NAV_CLASS)// |
- | gps[lenght+VEL_OFFSET]=c; | + | { |
+ | if(id==SVINFO_DATA) | ||
+ | gps.packetflag|=0x08; | ||
+ | if(id==SOL_DATA) | ||
+ | gps.packetflag|=0x04; | ||
+ | if(id==LLH_DATA) | ||
+ | gps.packetflag|=0x02; | ||
+ | if(id==VELNED_DATA) | ||
+ | gps.packetflag|=0x01; | ||
+ | } | ||
+ | if(!Gps.packetflag | ||
+ | { | ||
+ | TOGGLE_PIN;// | ||
+ | Gps=gps;//copy into the global variable | ||
+ | gps.packetflag=0;// | ||
+ | } // | ||
} | } | ||
- | } | ||
- | break; | ||
- | case 7: | ||
- | if(checksum_1==c) | ||
- | state=8; | ||
- | else | ||
state=0; | state=0; | ||
- | break; | + | } |
- | case 8: | + | if(state> |
- | if(checksum_2==c) | + | { |
- | if(class==NAV) // | + | checksum_1+=c; |
- | { | + | checksum_2+=checksum_1; |
- | if(id==LLH) | + | } |
- | gps.packetflag|=LLH_DATA; | + | else if(!state) |
- | if(id==VELNED) | + | { |
- | gps.packetflag|=VELNED_DATA; | + | checksum_1=0; |
- | } | + | checksum_2=0; |
- | if(!Gps.packetflag && gps.packetflag==REQUIRED_DATA)// | + | } |
- | { | + | |
- | toggle_pin;// | + | |
- | Gps=gps;// | + | |
- | } // | + | |
- | state=0; | + | |
- | } | + | |
- | if(state> | + | |
- | { | + | |
- | checksum_1+=c; | + | |
- | checksum_2+=checksum_1; | + | |
} | } | ||
- | else | + | </ |
+ | |||
+ | |||
+ | ===== header ===== | ||
+ | <code c> | ||
+ | typedef uint16_t u16; | ||
+ | typedef uint32_t u32; | ||
+ | typedef int32_t s32; | ||
+ | typedef struct | ||
{ | { | ||
- | checksum_1=0; | + | u32 time; // |
- | checksum_2=0; | + | s32 vnorth; // |
- | } | + | s32 veast; |
+ | s32 vdown; | ||
+ | s32 longitude; | ||
+ | s32 latitude; | ||
+ | s32 altitude; | ||
+ | u08 packetflag; | ||
+ | u08 status; | ||
+ | u08 nosats; | ||
+ | } ubx_gps_type; | ||
+ | #define REQUIRED_DATA 0x0F | ||
+ | #define LLH_DATA 0x02 | ||
+ | #define VELNED_DATA 0x12 | ||
+ | #define SVINFO_DATA 0x30 | ||
+ | #define SOL_DATA 0x06 | ||
+ | #define NAV_CLASS 0x01 | ||
+ | #define SATS_POS 5 | ||
+ | #define SOL_POS 42 | ||
+ | #define POS_OFFSET 40 | ||
+ | #define POS_START 12 | ||
+ | #define POS_END 25 | ||
+ | #define VEL_OFFSET 36 | ||
+ | #define VEL_START 20 | ||
+ | #define VEL_END 37 | ||
+ | #define SYNC_1 0xB5 | ||
+ | #define SYNC_2 0x62 | ||
+ | |||
+ | #define BAUDRATE 38400UL | ||
+ | #define BAUDDIV 32 | ||
+ | //#define BAUDDIV | ||
+ | #define TOGGLE_PIN PIND=0x20 // | ||
+ | |||
</ | </ | ||
+ |
code/interrupt_driven_ubx.1241136448.txt.gz · Last modified: 2009/05/01 00:07 by laurenceb