Advertisement
GoshPi

arduino UART

Jun 17th, 2019
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <Arduino.h>
  2. #define SYS_CLOCK_HZ 16000000
  3. #define BAUD 9600
  4.  
  5. byte* sendBits= (byte*)malloc(8);
  6. byte* buff=(char*) malloc(200);
  7. int pos,sendPos;
  8. char ch;
  9. void setup()
  10. {
  11. DDRA=0xFE;
  12. PORTA=0x04;
  13. Serial.begin(9600);
  14.  
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21.  
  22. void writeSerial(byte* data,int cnt)
  23. {
  24.  
  25.  
  26. //Set TX bit to low to start trasmission
  27. for(int i=0;i<cnt;i++)
  28. {
  29. for(int p=0;p<8;p++)
  30. {
  31.  
  32. sendBits[p]=( ( data[i] & (1 << p) ) >>p );
  33.  
  34. switch(sendBits[p])
  35. {
  36. case 1:
  37. Serial.write("1");
  38. break;
  39. case 0:
  40. Serial.write("0");
  41. break;
  42.  
  43.  
  44.  
  45.  
  46. }
  47.  
  48. }
  49. sendPos=0;
  50. PORTA &= ~(0x04);
  51. while(sendPos<8)
  52. {
  53. delayMicroseconds(1000000/BAUD);
  54. PORTA=sendBits[sendPos++] << 2;
  55.  
  56. }
  57. delayMicroseconds(1000000/BAUD);
  58. PORTA |= 0x04;
  59. }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. void loop()
  67. {
  68. pos=0;
  69. while(Serial.available())
  70. {
  71. ch=Serial.read();
  72. if(ch!=13 && ch!=10)
  73. buff[pos++]=ch;
  74.  
  75. }
  76. writeSerial(buff,pos);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement