发一个SPI驱动CH451的程序

/* **************************************************** * Func : C8051F330与CH451的SPI通信 * Date : 2007,July,24th ******************************************************/

#include "c8051F330.h" #include #include

sbit CH451_CS =P0^6; //片选开关

typedef unsigned char UCHAR; typedef unsigned int UINT;

void Delay1Ms(UINT time) { UINT i=0, j=0; for(i=0; i { for(j=0; j<200; j++); } }

void CH451_ON() //片选 { CH451_CS = 1; Delay1Ms(300); CH451_CS = 0; Delay1Ms(2000); } void CH451_OFF() //关 { Delay1Ms(2000); CH451_CS = 1; } //******************************************************** //SPI Config // Peripheral specific initialization functions, // Called from the Init_Device() function void SPI_Init() { SPI0CFG = 0x70; SPI0CN = 0x01; SPI0CKR = 0x98; }

void Port_IO_Init() { // P0.0 - SCK (SPI0), Open-Drain, Digital // P0.1 - MISO (SPI0), Open-Drain, Digital // P0.2 - MOSI (SPI0), Open-Drain, Digital // P0.3 - Unassigned, Open-Drain, Digital // P0.4 - Unassigned, Open-Drain, Digital // P0.5 - Unassigned, Open-Drain, Digital // P0.6 - Unassigned, Open-Drain, Digital // P0.7 - Unassigned, Open-Drain, Digital

// P1.0 - Unassigned, Open-Drain, Digital // P1.1 - Unassigned, Open-Drain, Digital // P1.2 - Unassigned, Open-Drain, Digital // P1.3 - Unassigned, Open-Drain, Digital // P1.4 - Unassigned, Open-Drain, Digital // P1.5 - Unassigned, Open-Drain, Digital // P1.6 - Unassigned, Open-Drain, Digital // P1.7 - Unassigned, Open-Drain, Digital

XBR0 = 0x02; XBR1 = 0x40; }

void Oscillator_Init() { OSCLCN |= 0x03; }

// Initialization function for device, // Call Init_Device() from your main program void Init_Device(void) { SPI_Init(); Port_IO_Init(); Oscillator_Init(); }

//******************************************************** UINT Xchg12(UINT a) //12bit num invert { UCHAR i=0; UINT b=0; b=(a&1); for(i=0; i<11; i++) { a>>=1; b<<=1; b+=(a&1); } return b; }

void SPI_Write(const UINT command) //发命令,法三次 { //0x0403,0x058C UINT tmpCmd = 0; UCHAR chPrev, chNext;

tmpCmd = Xchg12(command); chPrev = tmpCmd>>8; chNext = (tmpCmd & 0xFF);

CH451_ON(); Delay1Ms(300);

SPIF = 0; SPI0DAT = 0x00; while(SPIF ==0);

SPIF = 0; //发前面的8位 SPI0DAT = chPrev; while(SPIF == 0);

SPIF = 0; //发后面8位 SPI0DAT = chNext; while(SPIF == 0);

Delay1Ms(300); CH451_OFF();

} //------------------------- void OpenDisplay() // 使能显示驱动,使能键盘功能 { SPI_Write( 0x0403); } //------------------------- void SetDispParam() //设置显示参数,BCD译码方式 { SPI_Write( 0x058c); } //------------------------- void DisplayTest() { SPI_Write( 0x0f00 | 0x17 ); /* 向第1个数码管加载数据'H' */ SPI_Write( 0x0e00 | 0x0e ); /* 向第1个数码管加载数据'E' */ SPI_Write( 0x0d00 | 0x18 ); /* 向第1个数码管加载数据'L' */ SPI_Write( 0x0c00 | 0x19 ); /* 向第1个数码管加载数据'P' */ SPI_Write( 0x0b00 | 0x10 ); /* 向第1个数码管加载数据' ' */ SPI_Write( 0x0a00 | 0x14 ); /* 向第1个数码管加载数据'[' */ SPI_Write( 0x0900 | 0x88 ); /* 向第1个数码管加载数据'8.' */ SPI_Write( 0x0800 | 0x15 ); /* 向第1个数码管加载数据']' */ SPI_Write( 0x0600 | 0x30 ); /* 第3个和第4个数码管闪烁 */ }

void main() {

PCA0MD &= ~0x40; //watch dog Init_Device(); SPIEN = 1; //启动SPI OpenDisplay(); // 使能显示驱动,使能键盘功能 SetDispParam(); // 设置显示参数,BCD译码方式 DisplayTest(); //显示演示 }

顶楼主。


只有登录才能回复,可以选择微信账号登录