stc8a--al422B————01,硬件上RE已经接地了。

#include "stc8a8k.h"
#include <stdio.h>  
#include "stdlib.h"
#include "stdarg.h"
#include "string.h"  //str函数

//22.1184Mhz;
#define DataIn  P0
#define DataOut P2

sbit WCK  =P1^5;
sbit WEN  =P1^4;
sbit WRST =P1^3;
sbit OE   =P1^2;
sbit RRST =P1^1;
sbit RCK  =P1^0;
//一共8个数据
unsigned char tablecode[]={0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF7};

//****************************************************************
#define Uart1_Rec_Maxlength 100
unsigned char Global_Uart1_Rec[Uart1_Rec_Maxlength];//串口1接收空间,100个字节
#define Uart1_END_CODE   '#'  //帧尾自定义符号
unsigned char	  Uart1_Rec_Byte_Length=0;//动态长度,在0-最大Uart1_Rec_Maxlength=100之间
unsigned char		Uart1_Rec_Frame_Flag=0;//接收一帧标记,=1,接收到一帧数据,=0,没有接受一帧数据
unsigned char   Global_uart1cmdnum=0;//串口接收到的命令号

//************************
void UartInit(void);		//115200bps@11.0592MHz
void UART1_SendData(char dat);
void UartSendStr(char *p);
void Clear_Uart1_Rxbuff(void);


//打开写使能和写时钟
//P0不断发送数据0x12 0x34 0x56 0x78 0x9A 0xBC 0xDE
//打开接收使能和接收时钟;
//然后将数据放到P2口;
//关闭接收使能
//将数据放入数组中;
//将数据发送到串口中,打印出来;
//打开写时钟,继续输出。
void Delay1us()		//@22.1184MHz
{
	unsigned char i;
	i = 6;
	while (--i);
}
void Delay5ms()		//@22.1184MHz
{
	unsigned char i, j;

	i = 144;
	j = 157;
	do
	{
		while (--j);
	} while (--i);
}

void Delay1ms()		//@22.1184MHz
{
	unsigned char i, j;

	i = 29;
	j = 183;
	do
	{
		while (--j);
	} while (--i);
}

void Delay5us()		//@22.1184MHz
{
	unsigned char i;

	i = 35;
	while (--i);
}

//初始化脉冲
void InitAL422B()
{
	
	WEN=1;
	WRST =1;
	OE=1;
	RRST =1;
	Delay1ms();	
	
	WEN=0;
	WRST =0;
	Delay1ms();
	WRST =1;
	WEN=1;
	Delay1ms();	

	OE=0;
	RRST =0;//低电平复位,写和读的地址从0开始
	Delay1ms();	
  RRST =1;
	OE=1;

	Delay1ms();
}

//复位,低电平复位
void ResetWRST_AL422B(void)
{
	WEN=0;
	WRST =1;
  WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;  WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;

	WRST =1;
	WEN=1;
}
//复位,低电平复位
void ResetRRST_AL422B(void)
{
	OE=0;
	RRST =1;
  RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;  RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;

	RRST =1;	
	OE=1;
}


//写操作
//void I2C_SendByte(unsigned char Byte)
//{
//	unsigned char i;
//	for(i=0;i<8;i++)
//	{
//	I2C_SDA=Byte&(0x80>>i);			 //把Byte最高位取出来,赋给SDA
//    I2C_SCL=1;
//    I2C_SCL=0;
//	}
//}
//	unsigned char i;
//	for(i = 0 ; i < 8 ; i++)
//	{
//		SDA = dat & (0x80>>i);	//依次取出dat的第7、6、5、4……位进行发送
//		SCL = 1;
//		SCL = 0;
//	}

//unsigned char I2C_ReceiveByte(void)
//{
// 	unsigned char i,Byte=0x00;
//	I2C_SDA=1;
//	if(i=0;i<8;i++)
//	{
//	I2C_SCL=1;
//	if(I2C_SDA){Byte|=(0x80>>i);}		//相当于如果SDA读取的是1,则把Byte的最高位置1
//    I2C_SCL=0;
//	}
//	return Byte;
//} 

void WriteAL422B(unsigned char temp)
{
	char i=0;
	WEN=0;
	DataIn = temp;
	
	for(i=8;i>0;i--)
	{
		WCK=0;
		WCK=1;//上升沿,写入数据  I2C_SDA=DataIn&(0x80>>i);	
		Delay1us();
		WCK=0;
	}

	WCK=0;
	WEN=1;
}
//	if(I2C_SDA){Byte|=(0x80>>i);}		//相当于如果SDA读取的是1,则把Byte的最高位置1
unsigned char ReadAL422B()
{
	unsigned char temp=0x00;
	char i=0;
	
	DataOut=0x00;
	
	OE=0;
	
	for(i=0;i<8;i++)
	{
		RCK=0;	
 //DataOut	|=(0x80>>i);
		//
		RCK=1;//上升沿,写入数据
		RCK=0;
	}
temp = DataOut;
	RCK=0;
  OE=1;
	return  temp;
}

void main()
{
	int i=0;
	unsigned char ReadVal=0;
	
	P0M0=0x00;
	P0M1=0x00;
		P1M0=0x00;
	P1M1=0x00;
		P2M0=0x00;
	P2M1=0x00;
		P3M0=0x00;
	P3M1=0x00;
		P4M0=0x00;
	P4M1=0x00;
		P5M0=0x00;
	P5M1=0x00;
		P6M0=0x00;
	P6M1=0x00;
	
	P7M0=0x00;
	P7M1=0x00;
	
	
	InitAL422B();
	UartInit();//串口1,波特率115200,定时器2产生,stc8a8k,22.1184Mhz
	ES = 1;
  EA = 1;

	
	Delay5ms();
	Delay5ms();
	Delay5ms();
	Delay5ms();
	Delay5ms();
	Delay5ms();
	Delay5ms();
	Delay5ms();
	while(1)
	{
				WEN=0;//允许写操作
	      WRST =1; WRST =1; WRST =1; WRST =1; WRST =1; WRST =1;
        WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;  WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;
				WRST =1;WRST =1;WRST =1;WRST =1;WRST =1;WRST =1;
	      WEN=1;
				
				
				WEN=0;
				DataIn = 0xcd;
				
				for(i=7;i>=0;i--)
				{
					WCK=0;
					WCK=1;//上升沿,写入数据  I2C_SDA=DataIn&(0x80>>i);	
					Delay1us();
					WCK=0;
				}
        WEN=1;
				
			
				//ResetRRST_AL422B();//读复位
					OE=1;
					RRST =1;
					RRST =0;RRST =0;RRST =0;
				
				RCK=0;	RCK=0;	RCK=0;
					RCK=1;		RCK=1;		RCK=1;
						RCK=0;RCK=0;RCK=0;
				  RRST =0;RRST =0;RRST =0;  RRST =0;RRST =0;

					RRST =1;	RRST =1;	RRST =1;	
					OE=1;
	
				OE=0;
				for(i=0;i<8;i++)
				{
					RCK=0;	
					RCK=1;//上升沿,写入数据
					RCK=0;
				}
				UART1_SendData(DataOut);//串口发送
				RCK=0;
				OE=1;
				
				
								
		Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();				
		Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();				
		Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();				
		Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();
				
				
		Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();
		
	  if(P43==0)
		{
			Delay5ms();
			Delay5ms();
			Delay5ms();
			Delay5ms();
			if(P43==0)
			{
				while(P43==0);//等待松下按键
								
				//ResetWRST_AL422B();//写复位
				WEN=0;//允许写操作
	      WRST =1;
        WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;  WRST =0; WRST =0; WRST =0; WRST =0; WRST =0; WRST =0;
				WRST =1;
	      WEN=1;
				
				
				WEN=0;
				DataIn = 0x34;
				
				for(i=7;i>=0;i--)
				{
					WCK=0;
					WCK=1;//上升沿,写入数据  I2C_SDA=DataIn&(0x80>>i);	
					Delay1us();
					WCK=0;
				}
        WEN=1;
				
				
				
				//ResetRRST_AL422B();//读复位
					OE=1;
					RRST =1;
					RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;  RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;RRST =0;

					RRST =1;	
					OE=1;
				ReadVal=ReadAL422B();//读一个数据,
				UART1_SendData(ReadVal);//串口发送
			//	Delay1us();Delay1us();Delay1us();Delay1us();Delay1us();
				
//				ReadVal=ReadAL422B();
//				UART1_SendData(ReadVal);

//			//	Delay1us();Delay1us();Delay1us();Delay1us();Delay1us();
//				ReadVal=ReadAL422B();
//				UART1_SendData(ReadVal);
			//	Delay1us();Delay1us();Delay1us();Delay1us();Delay1us();
			}
		}
	}

}

//串口1,波特率115200,定时器2产生,
void UartInit(void)		//115200bps@22.1184Mhz
{
	SCON = 0x50;		//8位数据,可变波特率
	AUXR |= 0x01;		//串口1选择定时器2为波特率发生器
	AUXR &= 0xFB;		//定时器2时钟为Fosc/12,即12T
	T2L = 0xFC;		//设定定时初值
	T2H = 0xFF;		//设定定时初值
	AUXR |= 0x10;		//启动定时器2
}

//UART1 发送串口数据,一个字节
void UART1_SendData(char dat)
 {
     ES=0;           //关串口中断
     SBUF=dat;           
     while(TI!=1);   //等待发送成功
     TI=0;           //清除发送中断标志
     ES=1;           //开串口中断
}
//UART1 发送串口数据,字符串
void UartSendStr(char *p)
{
    while (*p)
    {
        UART1_SendData(*p++);
    }
}
//重写putchar函数,可以用printf函数
char putchar(char c)
{
     UART1_SendData(c);
     return c;
}
//串口1中断服务函数
void Uart1Isr() interrupt 4 using 1
{
		unsigned char cmdtemp=0;
    if (TI)
    {
        TI = 0;
    }
    if (RI)
    {
			Global_Uart1_Rec[Uart1_Rec_Byte_Length]=SBUF;//将数据放到缓冲区中
			Uart1_Rec_Byte_Length++;//递增,不断放到数组中
			if(Uart1_Rec_Byte_Length>Uart1_Rec_Maxlength-1) 
				Uart1_Rec_Byte_Length=0;
			RI = 0;    
    }
}

//清除串口1的内存
void Clear_Uart1_Rxbuff(void)
{
	unsigned char *p,i;
	p=Global_Uart1_Rec;
	for(i=0;i<Uart1_Rec_Maxlength;i++)
	{
		*p++=0;
	}
	Uart1_Rec_Byte_Length=0;
	Uart1_Rec_Frame_Flag=0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值