Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. // Lab 4 - RS-232 Serial Interface Waveform
  2. // Troy Dowling, A00817082
  3. // 2015-10-05
  4.  
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <inttypes.h>
  8.  
  9. #define BAUD_RATE_HZ        9600
  10. #define SAMPLE_RATE_HZ      38400
  11. #define SAMPLES_PER_PULSE   (SAMPLE_RATE_HZ / BAUD_RATE_HZ)
  12.  
  13. #define MARKL           0x00
  14. #define MARKH           0x00
  15. #define SPACEL          0xFF
  16. #define SPACEH          0x3F
  17.  
  18. void tputw(bool input)
  19. {
  20.         if(input)
  21.         {
  22.             // Mark
  23.             for(int i = SAMPLES_PER_PULSE; i; i--)
  24.             {
  25.                 putc(MARKL, stdout);
  26.                 putc(MARKH, stdout);
  27.             }
  28.         }
  29.         else
  30.         {
  31.             // Space
  32.             for(int i = SAMPLES_PER_PULSE; i; i--)
  33.             {
  34.                 putc(SPACEL, stdout);  
  35.                 putc(SPACEH, stdout);
  36.             }
  37.         }
  38. }
  39.  
  40. int main(void)
  41. {
  42.     // This is the string to pipe out over UART
  43.     char str[] = "Troy Dowling A00817082\r\n";
  44.    
  45.     // For each character in the string
  46.     for(int i = 0; i < sizeof(str)/sizeof(str[0]); i++)
  47.     {
  48.         // Output start bit
  49.         tputw(false);
  50.        
  51.         // Output each bit in the character
  52.         uint8_t mask = 0x01;
  53.         for(int i = 0; i < 8; i++)
  54.         {
  55.             if(str[i] & mask)
  56.                 tputw(true);
  57.             else
  58.                 tputw(false);
  59.             mask <<= 1;
  60.         }
  61.        
  62.         // Output the stop bit
  63.         tputw(true);
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement