温翠苹桃色追缉令:计算机并口模拟SPI通讯的C源程序设计代码实例
来源:百度文库 编辑:中财网 时间:2024/04/29 05:01:41
#include
#include
#include
#include
#include
#define LPT_PORT 0x378
#define CLR_WCK(X) {X=X&(~(1<<0)); outportb(LPT_PORT,X); } // data.0
#define SET_WCK(X) {X=X | (1<<0) ; outportb(LPT_PORT,X); }
#define CLR_BCK(X) {X=X&(~(1<<2)); outportb(LPT_PORT,X); } // data.2
#define SET_BCK(X) {X=X | (1<<2) ; outportb(LPT_PORT,X); }
#define CLR_DATA(X) {X=X&(~(1<<3)); outportb(LPT_PORT,X); } // data.3
#define SET_DATA(X) {X=X | (1<<3) ; outportb(LPT_PORT,X); }
#define FALSE 0
#define TRUE 1
void test_comm()
{
unsigned char data ;
data = 0;
printf("Please press enter to begin send data\n");
getch();
printf("Pull down WCK data.0\n");
CLR_WCK(data);
getch();
printf("Pull up WCK data.0\n");
SET_WCK(data);
getch();
printf("Pull down BCK data.2\n");
CLR_BCK(data);
getch();
printf("Pull up BCK data.2\n");
SET_BCK(data);
getch();
printf("Pull down DATA data.3\n");
CLR_DATA(data);
getch();
printf("Pull up DATA data.3\n");
SET_DATA(data);
getch();
}
// Note: the size of buffer to send must be dword multiple
// size is the number of bytes to send
void short_delay(int n)
{
int i;
for(i=0;i {int temp =0;}
}
int send_spi_data(unsigned char *buffer, unsigned long size)
{
unsigned char buff[1024];
unsigned char *buf=buff;
unsigned char data;
int i,j,k;
data =0;
if((size%4)!=0) return FALSE;
memcpy(buff,buffer,size);
do{
SET_WCK(data);
for(k=0;k<2;k++){
for(j=0;j<2;j++){
printf(".");
for(i=0;i<8;i++){
if((*buf)&0x80){
SET_DATA(data);
}else{
CLR_DATA(data);
}
short_delay(1);
// delay(1);
SET_BCK(data);
short_delay(1);
// delay(1);
CLR_BCK(data);
short_delay(1);
// delay(1);
*buf<<=1;
}
buf++;
size--;
}
// buf++;
// size--;
CLR_WCK(data);
}
SET_WCK(data);
}while(size>0);
return TRUE;
}
/*
void main()
{
int i;
unsigned char tmpdata[4];
tmpdata[0] = 0x34;
tmpdata[1] = 0x12;
tmpdata[2] = 0x56;
tmpdata[3] = 0x78;
// for(i=0;i<500;i++)
for(i=0;i<50;i++)
{
send_spi_data(tmpdata,4);
}
// test_comm();
}
*/
#include
#include
#include
#include
#define LPT_PORT 0x378
#define CLR_WCK(X) {X=X&(~(1<<0)); outportb(LPT_PORT,X); } // data.0
#define SET_WCK(X) {X=X | (1<<0) ; outportb(LPT_PORT,X); }
#define CLR_BCK(X) {X=X&(~(1<<2)); outportb(LPT_PORT,X); } // data.2
#define SET_BCK(X) {X=X | (1<<2) ; outportb(LPT_PORT,X); }
#define CLR_DATA(X) {X=X&(~(1<<3)); outportb(LPT_PORT,X); } // data.3
#define SET_DATA(X) {X=X | (1<<3) ; outportb(LPT_PORT,X); }
#define FALSE 0
#define TRUE 1
void test_comm()
{
unsigned char data ;
data = 0;
printf("Please press enter to begin send data\n");
getch();
printf("Pull down WCK data.0\n");
CLR_WCK(data);
getch();
printf("Pull up WCK data.0\n");
SET_WCK(data);
getch();
printf("Pull down BCK data.2\n");
CLR_BCK(data);
getch();
printf("Pull up BCK data.2\n");
SET_BCK(data);
getch();
printf("Pull down DATA data.3\n");
CLR_DATA(data);
getch();
printf("Pull up DATA data.3\n");
SET_DATA(data);
getch();
}
// Note: the size of buffer to send must be dword multiple
// size is the number of bytes to send
void short_delay(int n)
{
int i;
for(i=0;i {int temp =0;}
}
int send_spi_data(unsigned char *buffer, unsigned long size)
{
unsigned char buff[1024];
unsigned char *buf=buff;
unsigned char data;
int i,j,k;
data =0;
if((size%4)!=0) return FALSE;
memcpy(buff,buffer,size);
do{
SET_WCK(data);
for(k=0;k<2;k++){
for(j=0;j<2;j++){
printf(".");
for(i=0;i<8;i++){
if((*buf)&0x80){
SET_DATA(data);
}else{
CLR_DATA(data);
}
short_delay(1);
// delay(1);
SET_BCK(data);
short_delay(1);
// delay(1);
CLR_BCK(data);
short_delay(1);
// delay(1);
*buf<<=1;
}
buf++;
size--;
}
// buf++;
// size--;
CLR_WCK(data);
}
SET_WCK(data);
}while(size>0);
return TRUE;
}
/*
void main()
{
int i;
unsigned char tmpdata[4];
tmpdata[0] = 0x34;
tmpdata[1] = 0x12;
tmpdata[2] = 0x56;
tmpdata[3] = 0x78;
// for(i=0;i<500;i++)
for(i=0;i<50;i++)
{
send_spi_data(tmpdata,4);
}
// test_comm();
}
*/
C语言设计贪吃蛇的源程序
哪儿可以下载到[计算机图形学]的C语言源程序
模拟电子琴的 汇编源程序
哪里能找到c语言,或者C++的三维立体图画设计出的源程序
请问哪里可以下到 SPI I2C 等通讯标准的协议?
计算机报童问题的源程序
公安部门的计算机通讯
SPI接口的使用
SPI的主要功能
计算机并口
谁知道哪有计算机 二级C的 模拟题啊
请问计算机并口输出的电流多大?
计算机并口的响应时间是多少
并口通讯线引脚定义
关于C语言源程序的问题
急救:下拉菜单的C语言源程序
c语言源程序文件名的后缀是什么??
多功能闹钟的C语言源程序
急求C的一个源程序!~~
C语言源程序文件名的后缀是什么?
!!求教:模拟操作系统进程调度的VB源程序!!!
请问那里有出租车计价器的设计的源程序
计算机通讯专业的就业前景怎么样?
通讯方面计算机英语的文章翻译