Advertisement
kirill_76rus

route_control

Oct 10th, 2020
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. #include <intrinsics.h>
  2. #include<iostm8s003f3.h>
  3. #include<stdint.h>
  4. #include<stdlib.h>
  5. /*******************************************************************************/
  6.  
  7. /*block of define*/
  8.  
  9. #define   SetBit(reg, bit)          (reg) |= ((1U)<<(bit))        
  10. #define   ClearBit(reg,bit)       (reg) &= (~((1U)<<(bit)))
  11. #define   InvBit(reg, bit)          (reg) ^= ((1U)<<(bit))
  12. #define   BitIsSet(reg, bit)       (reg) & (((1U)<<(bit)) != 0U))
  13. #define   BitIsClear(reg, bit)    (((reg) & (((1U)<<(bit))) == 0))
  14. #define   TRUE  (1U)
  15. #define   FALSE (0U)
  16. #define   SIZE  (1U)
  17. #define OVERFLOW(cur, max)         (((cur)<(max))==1)
  18. /*******************************************************************************/
  19.  
  20. /*variable declaration block*/
  21. uint8_t count_recieve=0U; /*Count packs recieved*/
  22. typedef struct {
  23.   uint8_t sig;
  24.   uint16_t dest_id;
  25.   uint8_t cmd;
  26.   uint8_t sz;    
  27.   uint8_t data_ptr[249];/*we not using dinamical memory, because define maximal value of array*/
  28. } uart_recieve;
  29.  
  30. /*******************************************************************************/
  31.  
  32. /*function declaration block*/
  33.  
  34. static void clock_init(void);
  35. static void port_init(void);/*init port for module*/
  36. static void UART_init(void);/*init uart set*/
  37. static unsigned char addr_recognize(void);
  38. static void I2C_init(uint8_t address);/*I2C initialization*/
  39. static void write_packet(uint8_t pack);
  40. /*******************************************************************************/
  41.  
  42. /*block of interrupt function*/
  43.  
  44. __interrupt void UART1_RXE(void);
  45. __interrupt void I2C_ADDR(void);
  46.  
  47. /*******************************************************************************/
  48.  
  49. /*function definition block*/
  50.  
  51. static void clock_init(void){
  52.  
  53.    CLK_ICKR_HSIEN=1U;/*enable hight speed internal RC oscillator */
  54.    CLK_CKDIVR=0x1FU;
  55.    
  56. }
  57. /*****************/
  58. static void port_init(void){
  59.  
  60.   PD_DDR_DDR5=1U;/*UART TX as output*/
  61.   PB_DDR_DDR5=1U;/*SDA as output*/
  62.  
  63. }
  64. /*****************/
  65. static void UART_init(void){
  66.  
  67.   UART1_BRR2=0x03U;/*set baudra te 9600*/
  68.   UART1_BRR1=0x68U;/*with 0.02% error*/
  69.   UART1_CR2_RIEN=1U;/*reciever interrupt enable*/
  70.   UART1_CR2_TEN=1U;/*transmitter enable*/
  71.   UART1_CR2_REN=1U;/*reciever enable*/
  72.  
  73.   }
  74. /*****************/
  75. static unsigned char addr_recognize(void){
  76.  
  77.   PD_DDR=0x00U;/*ALL PORT D AS INPUT*/  
  78.   return  (PD_IDR);/*READ VALUE OF A PORT D*/
  79.  
  80. }
  81. /*****************/
  82. static void I2C_init(uint8_t address){
  83.  
  84.   I2C_CR1_ENGC=1U;/*general call enable*/
  85.   I2C_CR1_PE=1U;/*peripheral enable*/
  86.   I2C_CR2_POS=1U;/*acknowledge position of the next byte*/
  87.   I2C_CR2_ACK=1U;/*acknowledge byte enable*/
  88.   I2C_FREQR=4U;/*clock frequencies if 4 MHz for fast mode*/
  89.   I2C_OARL = (address << 4);/*set address, when defining with GPIO register*/
  90.   I2C_ITR_ITBUFEN=1U;/*TXE==1||RXNE==1 generates Event interrupt*/
  91.   I2C_ITR_ITERREN = 1U;/*interrupts enable*/
  92.  
  93. }
  94. /******************/
  95. static void write_packet(uint8_t pack){
  96.   uart_recieve temp;
  97.   switch(count_recieve){
  98.   case 0:{
  99.       temp.sig=pack;
  100.       break;
  101.   }
  102.   case 1:{
  103.       temp.dest_id=pack;
  104.       break;
  105.   }
  106.   case 2:{
  107.       temp.dest_id+=pack;
  108.     break;
  109.   }
  110.   case 3:{
  111.      temp.cmd=pack;
  112.      break;
  113.   }
  114.   case 4:{
  115.      temp.sz=pack;
  116.      break;
  117.   }
  118.   default:{
  119.     while(!OVERFLOW(count_recieve, 254U)){    /*while all data witout crc_tail not recieved, write data to array*/
  120.       temp.data_ptr[count_recieve - 4U] = pack;   /*write value to array*/
  121.    }
  122.     break;
  123.     }
  124.   }
  125. }
  126. /*******************************************************************************/
  127.  
  128. int main(void){
  129.  
  130.   /*block of initialization*/
  131.  
  132.   clock_init();
  133.   port_init();
  134.   UART_init();
  135.   I2C_init(addr_recognize());
  136.   __enable_interrupt();
  137.   return 0;
  138. }
  139.  
  140. /*******************************************************************************/
  141.  
  142. /*block of interrupt*/
  143.  
  144. __interrupt void UART1_RXE(void)
  145.      {
  146.        ++count_recieve;          /*increment counter of recieve packet*/
  147.        write_packet(UART1_DR);   /*write data to struct*/
  148.      }
  149. __interrupt void I2C_ADDR(void){
  150.   asm("nop");
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement