测试 PIC16F877A 《==》 MAX6902

来源:互联网 发布:excel两张表数据匹配 编辑:程序博客网 时间:2024/06/11 09:10
/* Define processor and include header file. */#include <sdcc-lib.h>#define __16f877a#include <pic16f877a.h>static __code unsigned int __at 0x2007 __CONFIG = \    _CPD_OFF    \   // EEPROM data is not code protected (doesn’t matter)  & _CP_OFF     \   // allows the program memory to be read  & _XT_OSC     \   // Can be clocked by crystal, or external clock  & _DEBUG_OFF  \   // lets the device run without the ICD tool  & _BODEN_OFF  \   // Low Voltage Brownout disabled (your choice)  & _PWRTE_ON   \   // Power up timer on for more reliable startup  & _LVP_OFF    \   // PORTB pins are available after programming  & _WDT_OFF;       // Crash protection feature disabled (for example)void putc(char ch){  while (!/*TXSTAbits.*/TRMT)    // make sure buffer full bit is high before transmitting    ;  TXREG = ch;}void puthex_l(unsigned char h){  if (h > 0x09)    h += 0x27;  putc(h + 0x30);}void puthex8(unsigned char h){  puthex_l(h >> 4);  puthex_l(h & 0x0f);}void max6902_init(){  RC0 = 1;      //MAX6902 CS pin. low active.  /*TRISAbits.*/TRISC0 = 0; // RC0 = SPI SS must be set as input  /*TRISCbits.*/TRISC3 = 0; // RC3 = SPI SCK(Master) must be set as output  /*TRISCbits.*/TRISC4 = 1; // RC4 = SPI SDI must be set as input  /*TRISCbits.*/TRISC5 = 0; // RC5 = SPI SDO must be set as output      /**< SSPM3:SSPM0: Synchronous Serial Port Mode Select bits   * 0000= SPI Master mode, clock = FOSC/4    * 0001= SPI Master mode, clock = FOSC/16    * 0010= SPI Master mode, clock = FOSC/64    * 0011= SPI Master mode, clock = TMR2 output/2    * 0100= SPI Slave mode, clock = SCK pin. SSpin control enabled.    * 0101= SPI Slave mode, clock = SCK pin. SSpin control disabled. SScan be used as I/O pin.    * 0110= I2C Slave mode, 7-bit address    * 0111= I2C Slave mode, 10-bit address    * 1000= I2C Master mode, clock = FOSC/ (4 * (SSPADD+1))    * 1011= I2C Firmware Controlled Master mode (slave idle)    * 1110= I2C Firmware Controlled Master mode, 7-bit address with START and STOP bit interrupts enabled   * 1111= I2C Firmware Controlled Master mode, 10-bit address with START and STOP bit interrupts enabled   * 1001, 1010, 1100, 1101= Reserved   */  /*SSPCONbits.*/SSPM0 = 0;         // SPI Master mode, clock = FOSC/16  /*SSPCONbits.*/SSPM1 = 1;  /*SSPCONbits.*/SSPM2 = 0;  /*SSPCONbits.*/SSPM3 = 0;  /*SSPCONbits.*/SSPEN = 1;         // 1 = Enables the serial port and configures the SDA and SCL pins as the source of the serial port pins  SSPIF = 0;  CKE = 1;                          // Transmit data on rising edge of clock  SMP = 0;                          // Input sampled at middle of data output time}unsigned char max6902_read_byte(unsigned char cmd){  RC0 = 0;                      //MAX6902 CS pin. low active.    /*PIR1bits.*/SSPIF = 0;       // clear SSP interrupt bit  SSPBUF = cmd;                 // send databyte  while (!/*PIR1bits.*/SSPIF)   // Wait for interrupt flag to go high indicating transmission is complete    ;  /*PIR1bits.*/SSPIF = 0;  SSPBUF = 0xff;  while (!/*PIR1bits.*/SSPIF)   // Wait for interrupt flag to go high indicating transmission is complete    ;  RC0 = 1;                      //MAX6902 CS pin. low active.  return SSPBUF;                // Data from eeprom is now in the SSPBUF so return that value}void max6902_write_byte(unsigned char cmd, unsigned char data){  RC0 = 0;                      //MAX6902 CS pin. low active.    /*PIR1bits.*/SSPIF = 0;       // clear SSP interrupt bit  SSPBUF = cmd;                 // send databyte  while (!/*PIR1bits.*/SSPIF)   // Wait for interrupt flag to go high indicating transmission is complete    ;  /*PIR1bits.*/SSPIF = 0;  SSPBUF = data;  while (!/*PIR1bits.*/SSPIF)   // Wait for interrupt flag to go high indicating transmission is complete    ;  RC0 = 1;                      //MAX6902 CS pin. low active.}void main(void) {  int i = 0;  unsigned char cmd;    /*TRISCbits.*/TRISC6 = 0; // RC6 = TX out  /*TRISCbits.*/TRISC7 = 1; // RC7 = RX in    /**<   * 1. Initialize the SPBRG register for the appropriate baud rate.    *    If a high speed baud rate is desired, set bit BRGH.   * 2. Enable the asynchronous serial port by clearing bit SYNC and setting bit SPEN.   * 3. If interrupts are desired, then set enable bit TXIE.   * 4. If 9-bit transmission is desired, then set transmit bit TX9.   * 5. Enable the transmission by setting bit TXEN, which will also set bit TXIF.   * 6. If 9-bit transmission is selected, the ninth bit should be loaded in bit TX9D.   * 7. Load data to the TXREG register.   * 8. If using interrupts, ensure that GIE and PEIE (bits 7 and 6) of the INTCON register are set.   */  /*TXSTAbits.*/SYNC = 0;       // USART Mode Select bit. 1 = Synchronous mode  /*TXSTAbits.*/BRGH = 0;       // select low speed Baud Rate (see baud rate calcs below)  /*TXSTAbits.*/TX9 = 0;        // select 8 data bits  /*TXSTAbits.*/TXEN = 1;       // Transmit Enable bit. 1 = Transmit enabled    /*RCSTAbits.*/SPEN = 1;       // 1= Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)  /*RCSTAbits.*/RX9 = 0;        // select 8 data bits  /*RCSTAbits.*/CREN = 1;       // Receiver Enable bit. 1 = Receiver enabled    /**< calculate values of SPBRG based on the desired baud rate   *   * For 8 bit Async mode with BRGH = 0: Desired Baud rate = Fosc / 64(SPBRG + 1)   * For 8 bit Async mode with BRGH = 1: Desired Baud rate = Fosc / 16(SPBRG + 1)   *   * For our example,we will use BRGH=0,Fosc=10Mhz and we want baud rate=9600   *   *  9600 = Fosc / 64(SPBRG + 1)   *  9600 = Fosc / 64(X + 1)   *  9600 = Fosc / 64X + 64   *  9600(64X + 64) = Fosc   *  X = [Fosc / (9600)(64)] - 1   *  X = [10000000 / (9600)(64)] - 1   *  X = SPBRG = 15.28 (round to 25)   */  SPBRG = 15;  /*PIR1bits.*/RCIF = 0;        // make sure receive interrupt flag is clear  //PIE1bits.RCIE=1;        // enable UART Receive interrupt  //INTCONbits.PEIE = 1;    // Enable peripheral interrupt  //INTCONbits.GIE = 1;     // enable global interrupt  //TXIE = 0; not use TX interrupt.  //RCIE = 0; not use RX interrupt.  max6902_init();  while (1) {    while (!RCIF)      ;    cmd = RCREG;    RCIF = 0;    if (cmd == 0x20) {      cmd = max6902_read_byte(0x85);      puthex8(cmd);      putc(':');      cmd = max6902_read_byte(0x83);      puthex8(cmd);      putc(':');      cmd = max6902_read_byte(0x81);      puthex8(cmd);      putc('\r');    }    if (cmd == 0x0d) {      max6902_write_byte(5, 0);      max6902_write_byte(3, 0);      max6902_write_byte(1, 0);    }  }}

0 0
原创粉丝点击