Advertisement
kirill_76rus

uart

Oct 6th, 2020
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <intrinsics.h>
  2. #include<iostm8s003f3.h>
  3. #include<stdint.h>
  4. /**********************/
  5. /*define block*/
  6.  
  7. #define   SetBit(reg, bit)          reg |= (1<<bit)        
  8. #define   ClearBit(reg, bit)       reg &= (~(1<<bit))
  9. #define   InvBit(reg, bit)          reg ^= (1<<bit)
  10. #define   BitIsSet(reg, bit)       ((reg & (1<<bit)) != 0)
  11. #define   BitIsClear(reg, bit)    ((reg & (1<<bit)) == 0)
  12.  
  13. /**********************/
  14. /*variable block*/
  15. uint8_t dev_addr;/*dev address for I2C*/
  16.  
  17. /*************************/
  18. /*function declaration block*/
  19. static void clock_init(void);
  20. static void port_init(void);/*init port for module*/
  21. static void uart_init(void);/*init uart set*/
  22. static void uart_send(unsigned char data);
  23. __interrupt void UART1_RXE(void);
  24. /*static void i2c_init(void);*/
  25.  /************************/
  26. /*function definition block*/
  27. static void clock_init(void){
  28.    CLK_ICKR|=(1<<0);/*enable hight speed internal RC oscillator */
  29.    CLK_CKDIVR|=(1<<4|1<<3|1<<2|1<<1|1<<0);/*enable RC prescale/1*/
  30. }
  31. static void port_init(void){
  32.   PD_DDR|=(1<<5);/*UART TX as output*/
  33.   PB_DDR|=(1<<5);/*SDA as output*/
  34. }
  35. static void uart_init(void){
  36.   UART1_BRR2=0x03U;/*set baudrate 9600*/
  37.   UART1_BRR1=0x68U;/*with 0.02% error*/
  38.   UART1_CR2|=(1<<5);/*reciever interrupt enable*/
  39.   UART1_CR2|=(1<<3);/*transmitter enable*/
  40.   UART1_CR2|=(1<<2);/*reciever enable*/
  41. }
  42. /*uint8_t read_addr(void){
  43. return 0xff;
  44. }*/
  45. /*static void uart_send(unsigned char data){
  46.   while(!UART1_SR_TXE){
  47.     UART1_DR=data;
  48.   }
  49. }*/
  50. int main(void){
  51.   clock_init();
  52.   port_init();
  53.   uart_init();
  54.   __enable_interrupt();
  55.   PD_ODR|=(1<<3);
  56.  
  57.   while(1){
  58. /*uart_send(0xffU);*/
  59.     PD_ODR^=(1<<3);
  60.    
  61.   }
  62. }
  63. __interrupt void UART1_RXE(void)
  64.      {
  65.           UART1_DR=UART1_DR;
  66.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement