Advertisement
Guest User

main.c

a guest
Sep 14th, 2012
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. #include <xc.h>
  2. #include "16x2LCDHD44780.h"
  3. #include "msspi2c.h"
  4.  
  5. __CONFIG(LVP_OFF & FCMEN_OFF & IESO_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
  6.  
  7. /*
  8. Processor Type: PIC16F887
  9.  
  10. Configuration Word 1
  11.  
  12. Debug Disabled (default - controlled by PICkit 2)
  13. Low Voltage Programming Disabled (LVP)
  14. Fail Safe Clock Monitor Disabled (FCMEN)
  15. Internal External Oscillator Switchover Disabled (IESO)
  16. Brown Out Reset Disabled (BOR)
  17. Program Code External Read Protect Disabled (default)
  18. Data EEPROM External Read Protect Disabled (default)
  19. RE3 has MCLR function (default)
  20. Power Up Timer On (PWRTE)
  21. Watchdog Timer Off (WDTE)
  22. High Speed Oscillator Mode Enabled (FOSC_HS)
  23.  
  24. Configuration Word 2
  25.  
  26. Brown Out Reset Voltage 4V (default)
  27. Program Memory Write Protect Disabled (default)
  28. */
  29.  
  30. #define _XTAL_FREQ 16000000
  31. #define _I2C_BAUD 400000
  32. #define _UART_BAUD 31250
  33. #define _I2C_BRG ((_XTAL_FREQ / _I2C_BAUD) / 4) - 1
  34. #define _UART_BRG ((_XTAL_FREQ / _UART_BAUD) / 16) - 1
  35.  
  36. void main(void)
  37. {
  38.     PORTA = 0x00;               //clear PORTA latch
  39.     PORTB = 0x00;               //clear PORTB latch
  40.     PORTC = 0x00;               //clear PORTC latch
  41.     PORTD = 0x00;               //clear PORTD latch
  42.     PORTE = 0x00;               //clear PORTE latch
  43.     ANSEL = 0x00;               //all port pins digital I/O
  44.     ANSELH = 0x00;
  45.     TRISA = 0x00;               //RA0-RA5 output
  46.     TRISB = 0x00;               //RB0-RB7 output
  47.     TRISC = 0b11011001;
  48.     /*
  49.     RC0 - Write Enable (I2C EEPROM)
  50.     RC1 - Output
  51.     RC2 - Output
  52.     RC3 - SCL
  53.     RC4 - SDA
  54.     RC5 - Output
  55.     RC6 - UART TxD
  56.     RC7 - UART RxD
  57.     */
  58.     TRISD = 0x00;               //RD0-RD7 output
  59.     TRISE = 0x00;               //RE0-RE2 output
  60.    
  61.     //I2C Initialization
  62.    
  63.     SSPADD = _I2C_BRG;          //init baud rate for I2C
  64.     SMP = 0x00;                 //enable slew rate control for 400kHz operation
  65.     SSPCON2 = 0xFF;             //everything idle
  66.     SSPCON = 0b00101000;        //I2C Master Mode, Baud = Fosc / 4*(SSPADD+1)
  67.    
  68.     //UART Initialization
  69.    
  70.     BAUDCTL = 0x00;             //non-inverted data on TxD/RxD, disable baud rate detection
  71.     SPBRG = _UART_BRG;          //init baud rate for serial port
  72.     SPBRGH = 0x00;
  73.     TXSTA = 0b00100100;         //enable tx, high speed brg
  74.     RCSTA = 0b10010000;         //enable rx, enable serial port
  75.    
  76.     i2cStartBit();
  77.    
  78.     while(1);
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement