Advertisement
Guest User

Untitled

a guest
Mar 18th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #define F_CPU 16000000
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include "common/mavlink.h"
  5. #include "ardupilotmega/ardupilotmega.h"
  6.  
  7. #define rxEN 1
  8. #define txIEN 1
  9. #define drIEN 1
  10. #define txEN 1
  11. #define async 0x00
  12. #define noParity 0x00
  13. #define oneStop 0x00
  14. #define chSize8 0x03
  15. #define BAUD 57600
  16. #define BAUDPRESCALAR ((F_CPU/(BAUD))/16 - 1) //Register Values
  17.  
  18. typedef struct{
  19.    
  20.         uint8_t sysid;
  21.         uint8_t compid;
  22.         uint16_t len;
  23.         uint8_t bytes;
  24.         float data[2];
  25.         uint8_t* pData;
  26.         uint8_t dataBuffer[MAVLINK_MAX_PACKET_LEN];
  27.         uint8_t lenCounter;
  28.         mavlink_message_t* msg;
  29.        
  30. }mavlink_data_t;
  31.  
  32. mavlink_data_t mavlink_data;
  33.  
  34.  
  35. void sendData_UART();
  36. void initUART(uint8_t umsel, uint8_t upm, uint8_t usbs, uint8_t ucsz); //Prototypes
  37.  
  38.  
  39. int main(void)
  40. {
  41.     mavlink_data.sysid      = 1;
  42.     mavlink_data.compid     = 100;
  43.     mavlink_data.bytes      = 8;
  44.     mavlink_data.data[0]    = 566;
  45.     mavlink_data.data[1]    = 123;
  46.     *mavlink_data.pData = mavlink_data.data[0];
  47.     mavlink_data.lenCounter = 0;
  48.    
  49.     initUART(async, noParity, oneStop, chSize8); //Initiates UART 8-N-1
  50.     sei();
  51.  
  52.     mavlink_msg_data64_pack(mavlink_data.sysid, mavlink_data.compid, mavlink_data.msg, mavlink_data.data[0], mavlink_data.bytes, mavlink_data.pData);
  53.    
  54.     mavlink_data.len = mavlink_msg_to_send_buffer(mavlink_data.dataBuffer, mavlink_data.msg);
  55.    
  56.     sendData_UART();
  57.    
  58.     while(1){};
  59. }
  60.  
  61. void initUART(uint8_t umsel, uint8_t upm, uint8_t usbs, uint8_t ucsz){
  62.     //Initiates UART Baud and sets it to a 8-N-1 setting
  63.     UBRR0 = (uint8_t)(BAUDPRESCALAR);
  64.    
  65.     UCSR0C = (umsel << UMSEL00) |
  66.              (upm   << UPM00)   |
  67.              (usbs  << USBS0)   |
  68.              (ucsz  << UCSZ00);
  69. }
  70.  
  71. void sendData_UART(){
  72.     UCSR0B |= ((txIEN << TXCIE0) |
  73.                (drIEN << UDRIE0) |
  74.                (txEN  << TXEN0));
  75. }
  76.  
  77. ISR(USART0_UDRE_vect){
  78.     //If lengthCounter != len
  79.     //Else send disable interrupt
  80.     if(mavlink_data.lenCounter != mavlink_data.len){
  81.         UDR0 = mavlink_data.dataBuffer[mavlink_data.lenCounter];
  82.         mavlink_data.lenCounter++;
  83.     }
  84.     else{
  85.         UCSR0B &= ~(drIEN << UDRIE0);
  86.         mavlink_data.lenCounter = 0;
  87.     }
  88. }
  89.  
  90. ISR(USART0_TX_vect){
  91.     //Disables TX and interrupt for power consumption
  92.     UCSR0B &= ~((txIEN << TXCIE0) |
  93.                 (txEN  << TXEN0));
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement