当前位置:网站首页>stc8a--al422B————01,硬件上RE已经接地了。
stc8a--al422B————01,硬件上RE已经接地了。
2022-08-06 13:58:00 【ooolmf】
#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); //[email protected]
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) //[email protected]
{
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;
}
边栏推荐
猜你喜欢

Latex论文写作小技巧记录,不断更新

Rocket MQ Crash-Safe机制浅析
![[Blue Bridge Cup Training 100 Questions] Scratch ticket change, Blue Bridge Cup scratch competition special prediction programming questions, training simulation practice questions No. 23](/img/98/e00ef06106293b8df8288497692d04.png)
[Blue Bridge Cup Training 100 Questions] Scratch ticket change, Blue Bridge Cup scratch competition special prediction programming questions, training simulation practice questions No. 23

This article will take you to understand the technical principles of CDN!

Redis installation

【 TypeScript will learn will be 】 you must know all about TypeScript

机器学习笔记(吴恩达老师)

Unity2D horizontal version of the game tutorial 10 - scene control

IO的阻塞和非阻塞、同步和异步

智慧城市系列-1
随机推荐
gcc安装问题总结
Validate date format
ERC4907 的到来 会给 NFT 带来哪些变革?
Micro service database depots design solutions (cross-database associated query, distributed transaction processing)
智慧城市系列-1
MODBUS to PROFINET gateway to connect power intelligent monitoring instrument to PROFINET network case
winbox管理端口多少?
梅科尔工作室OpenHarmony设备开发培训笔记-第5章学习笔记
运筹说 第71期|论文速读之时间背包问题
unity2D横版游戏教程10-场景控制
The basic process used by mosquitto and some problems encountered
使用 Amazon Lambda Web Adapter 在 Amazon Lambda 上构建 Web 应用
RPC 基础系列
巴比特 | 元宇宙每日必读:什么是中国特色的元宇宙之路?边界和机会在哪里?...
Golang sync.Pool
The most complete technical guide for 3D format conversion tool HOOPS Exchange (1): 4 major functional characteristics and typical usage scenarios
解决创建虚拟机时No DEFAULT or UI configuration directive found问题
Learn more TypeScript 】 【 TypeScript namespace
Interface test CURL replication and postman's Code function
DC-9--vulnhub靶场