本帖最后由 wanghe334 于 2015-3-26 22:39 编辑
SPI数据发送例程:
- /*
- * File: main_xc16.c
- * Author: Aric Wang
- *
- * Created on 2015 3.24
- * DESC: Demo code for PIC24F16KL402 SPI communication.
- */
- #include "xc.h"
- _FBS
- (
- BWRP_OFF & // Boot Segment Write Protect (Disabled)
- BSS_OFF // Boot segment Protect (No boot flash segment)
- )
- //_FGS
- //(
- // GWRP_OFF & // General Segment Flash Write Protect (General segment may be written)
- // GSS0_OFF // General Segment Code Protect (No Protection)
- //)
- _FOSCSEL
- (
- FNOSC_LPRC & // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
- // SOSCSRC_ANA & // SOSC Source Type (Analog Mode for use with crystal)
- // LPRCSEL_HP & // LPRC Power and Accuracy (High Power/High Accuracy)
- IESO_ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
- )
- _FOSC
- (
- // POSCMD_HS & // Primary Oscillator Mode (Primary oscillator disabled)
- OSCIOFNC_ON & // CLKO Enable Configuration bit (CLKO output signal enabled)
- POSCFREQ_MS & // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
- SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
- FCKSM_CSECME // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
- )
- _FWDT
- (
- // WDTPS_PS32768 & // Watchdog Timer Postscale Select bits (1:32768)
- FWPSA_PR128 & // WDT Prescaler bit (WDT prescaler ratio of 1:128)
- FWDTEN_OFF & // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
- WINDIS_OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
- )
- // Warning:
- // Always enable MCLRE_ON config bit setting so that the MCLR pin function will
- // work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
- // programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
- // functionality, a high-voltage ICSP tool will be required to re-program the
- // part in the future.
- _FPOR
- (
- BOREN_BOR3 & // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
- PWRTEN_ON & // Power-up Timer Enable (PWRT enabled)
- I2C1SEL_PRI & // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
- BORV_V18 & // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
- MCLRE_ON // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
- )
- _FICD
- (
- ICS_PGx3 // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
- )
- unsigned char data_temp;
- void init_channel()
- {
- //Init SPI pins.
- //As master SDI1 is input, SDO1 is ouput, SCK1 is output, SS is output.
- ;
- }
- void init_SPI1_IO()
- {
- TRISBbits.TRISB10 = 1; //Set SDI1 as an input.
- TRISBbits.TRISB13 = 0; //Set SDO1 as an output.
- TRISBbits.TRISB11 = 0; //Set SCK1 as an output.
- TRISBbits.TRISB15 = 0; //Set SS1 as an output.
- TRISAbits.TRISA0 = 0;
- TRISBbits.TRISB3 = 0;
- }
- void init_SPI1(void)
- {
- // SPI1CON1 setting
- SSP1STAT =0b0000000010000000; //0b 0000 0000 1000 0000 0x0000;
- SSP1CON1 = 0b0000000010100001; //0x0021;//
- //SSP1CON3bits.BOEN = 0;
- SSP1ADD = 0x000f;
- PADCFG1 = 0x0c00; //0b0000 1100 0000 0000
- // IFS1bits.SSP1IF = 0;
- // IEC1bits.SSP1IE = 1;
- // IPC4bits.SSP1IP = 4;
- }
- void tx_spi_data(unsigned char dat)
- {
- SSP1BUF = dat;
- //while(!IFS1bits.SSP1IF);
- while(SSP1STATbits.BF)
- // IFS1bits.SSP1IF = 0;
- data_temp = SSP1BUF;
- }
- void delay(unsigned int uiCnt)
- {
- int i,j;
- for(i=0;i
- for(j=0;j<10;j++);
- }
- int main(void) {
- //Data to be sent.
- unsigned int tx[10]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- int i;
- delay(100);
- //SSP1BUF=0;
- init_channel();
- init_SPI1_IO();
- init_SPI1();
- //SSP1BUF=0;
- LATBbits.LATB3 = 1;
- // SSP1BUF = 0;
- LATBbits.LATB15 = 1;
- while(1)
- {
- for(i=0;i<9;i++)
- {
- tx_spi_data(tx[i]);
- // LATBbits.LATB3 ^= 1;
- }
- // delay(2);
- LATAbits.LATA0 ^= 1;
- }
- return 0;
- }
- void __attribute__((interrupt,no_auto_psv)) _SPI1Interrupt(void)
- {
- //IFS1bits.SSP1IF = 0; //Clear Interrupt status of SPI1
- // IEC1bits.SSP1IE = 1;
- }
复制代码
0
|
|
|
|