Guest User

Transmit UART

a guest
Apr 22nd, 2013
2,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Willem
  4.  *
  5.  * Created on April 22, 2013, 2:23 PM
  6.  */
  7. #include <xc.h>
  8. #include <pic12f675.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. /*
  12.  *
  13.  */
  14.  
  15. // CONFIG
  16. #pragma config FOSC = INTRCIO  // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
  17. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
  18. #pragma config PWRTE = ON       // Power-Up Timer Enable bit (PWRT enabled)
  19. #pragma config MCLRE = OFF      // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
  20. #pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
  21. #pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
  22. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  23.  
  24. // ========================================================================
  25. // ===== DEFINITIONS ======================================================
  26. // ========================================================================
  27.  
  28. // ===== Constants ========================================================
  29. #define     Tx_Pin      GP4     // Don't change this
  30. #define     BAUD        9600    // Adjust baud here
  31.  
  32. #define     _XTAL_FREQ  4.0
  33. #define     uart_time_delay()   __delay_us(1000000/BAUD)  // Delay = 1/Baud
  34.  
  35. // ===== Prototypes ======================================================
  36.  
  37. // Initializations
  38. void setup_variables(void);
  39. void setup_ports(void);
  40.  
  41. // Subroutines
  42. void read_sensor(void);
  43. void uart_tx_bit_bang(unsigned char val);
  44.  
  45. // ===== Variables =======================================================
  46. unsigned char val;
  47. unsigned int sysclock;
  48.  
  49. // ========================================================================
  50. // ===== Main =============================================================
  51. // ========================================================================
  52. int main(void) {
  53.     // Initializations
  54.     setup_variables();
  55.     setup_ports();
  56.    
  57.     // Main Loop
  58.     while(1) {
  59.         read_sensor();
  60.         uart_tx_bit_bang(val);
  61.     }
  62. }
  63.  
  64. // ========================================================================
  65. // ===== Subroutines ======================================================
  66. // ========================================================================
  67. // Read analog sensor and do A/D conversion
  68. void read_sensor(void) {
  69.     val = 22;
  70. }
  71.  
  72. // Method for transmitting value using serial bit banging
  73. void uart_tx_bit_bang(unsigned char val) {
  74.     unsigned char i;
  75.     Tx_Pin = 0;                         // Start bit
  76.     uart_time_delay();
  77.     for ( i = 8 ; i != 0 ; --i ) {
  78.         if (val & 0x01) Tx_Pin = 1;   // Begin with MSB
  79.         else            Tx_Pin = 0;
  80.         val >>= 1;
  81.         uart_time_delay();
  82.         }
  83.     Tx_Pin = 1;                         // Stop bit
  84.     uart_time_delay();
  85. }
  86.  
  87. // ========================================================================
  88. // ===== Setup Routines ===================================================
  89. // ========================================================================
  90. // Set variables to initial states
  91. void setup_variables(void) {
  92.     Tx_Pin = 1;
  93.     val = 0;
  94. }
  95.  
  96. void setup_ports(void) {
  97.     TRISIO = 0x11001101;
  98.     OPTION_REG = 0x11010010;
  99.     ANSEL = 0x01010001;
  100.     TMR0 = 0;
  101.     GPIO = 0;
  102.     CMCON = 0x00000111;
  103.     ADON = 0x1;
  104.     ADFM = 0x1;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment