📄 uart-io.c
字号:
//UCSRA |= 0x40;
//UDR=temp;
//while (!(UCSRA & 0x40));
number++;
if (number == 255) //buffer1读取完毕,主存转入buffer1,开始读取buffer2
{
PORTB |= 0x10; //PB4(DF_CS)=1
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//主存转入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10; //PB4(DF_CS)=1
page++;
PORTB &= ~0x10; //PB4(DF_CS)=0
write_SPI(BUFFER_2_READ); //开始读取buffer2
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
//write_SPI(0x00);
}
if (number == 510)//buffer2读取完毕,主存转入buffer2,开始读取buffer1
{
PORTB |= 0x10; //PB4(DF_CS)=1
number = 0;
PORTB &= ~0x10; //PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B2_XFER);//主存转入buffer2
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10; //PB4(DF_CS)=1
page++;
PORTB &= ~0x10; //PB4(DF_CS)=0
write_SPI(BUFFER_1_READ); //开始读取buffer1
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
//write_SPI(0x00);
}
if (upordown==4)
{
end();
}
else if (upordown==5)
{
jud();
}
if (beg==1)
{
Programme();
}
else if (beg==2)
{
Programmeeprom();
}
if (page==2048||rxtxend==3) //flash全部读取完成,停止
{
if (prof==1)
{
if (temp2==1) //如果有EEPROM数据,则
{
page--;
number=0;
rxtxend=0;
temp2=0;
PORTB |= 0x10;//PB4(DF_CS)=1
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//将第一页装入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B2_XFER);//将第二页装入buffer2
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_READ);//准备好从buffer1读取数据
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
}
else
{
if (temp3==1&&pror==1) //如果有熔丝位数据,则
{
page--;
number=0;
rxtxend=3;
temp3=0;
PORTB |= 0x10;//PB4(DF_CS)=1
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//将第一页装入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_READ);//准备好从buffer1读取数据
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
}
else if (temp3==0&&pror==1)
{
if (number==1)
{
rosh=temp;
}
else if (number==2)
{
rosl=temp;
}
else if (number==3)
{
lock=temp;
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
rxtxend=0;
//UCSRB &= ~0x40; //关闭UART发送中断使能
//TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
}
}
else
{
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
//UCSRB &= ~0x40; //关闭UART发送中断使能
//TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
}
}
}
else if (prof==0&&proe==1)
{
if (temp3==1&&pror==1)
{
temp2=0;
page--;
number=0;
rxtxend=3;
temp3=0;
PORTB |= 0x10;//PB4(DF_CS)=1
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//将第一页装入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay1(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_READ);//准备好从buffer1读取数据
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
}
else if (temp3==0&&pror==1)
{
if (number==1)
{
rosh=temp;
}
else if (number==2)
{
rosl=temp;
}
else if (number==3)
{
lock=temp;
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
rxtxend=0;
//UCSRB &= ~0x40; //关闭UART发送中断使能
//TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
Write_ros(); //写熔丝位
}
}
else
{
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
//UCSRB &= ~0x40; //关闭UART发送中断使能
//TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
}
}
else if (prof==0&&proe==0)
{
return;
}
}
}
while(state==1);
TCCR0 = 0x05;
}
void DownFromPC(void)
{
delay(2000);
PORTB |= 0x10; //PB4(DF_CS)=1
PORTB &= ~0x10; //PB4(DF_CS)=0
chip_unlock(); //片解锁
PORTB |= 0x10; //PB4(DF_CS)=1
//~~~~~~~~~~~~~~~~~~~~~~~~~写DataFlash~~~~~~~~~~~~~~~~~~~~~~~~~~//
ts=0; //参数位清零
//page=0;
number=0; //字节数清零
rxtxend=0; //参数位清零
PORTB |= 0x10;//PB4(DF_CS)=1
delay(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_WRITE);
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00); // 设置好buffer1 write
state=5; //设置状态
TCCR0 = 0x00; //关闭定时/计数器0,即关闭键盘采样
uart0_init(); //UART初始化
UCSRB |= 0x80; //UART接收中断使能
if (pror==1) //判断是否是下载熔丝位内容
{
while (state==5) //等待中断发生
{
if (number==3) //下载完3个字节后跳出
break;
}
state=2; //状态清2
number=0;
upordown=0;
UCSRB &= ~0x80; //关闭UART接受中断使能
TCCR0 = 0x05; //启动定时/计数器0
PORTB |= 0x10; //PB4(DF_CS)=1
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(B1TOMM_WITHOUT_ERASE);//将第一页存入主存
write_SPI((char)(page>>7));
write_SPI((char)(page<<1));
write_SPI(0x00);
}
else
{
while (state==5); //进入循环,等待中断发生
}
PORTB |= 0x10; //PB4(DF_CS)=1
delay1(20);
}
void ReadDataFlash(void)
{
delay(2000);
PORTB |= 0x10; //PB4(DF_CS)=1
PORTB &= ~0x10; //PB4(DF_CS)=0
chip_unlock(); //片解锁
PORTB |= 0x10; //PB4(DF_CS)=1
delay(1000);
//~~~~~~~~~~~~~~~~~~~~~~~~~读DataFlash~~~~~~~~~~~~~~~~~~~~~~~~~~//
ts=0;
page=0;
number=0;
rxtxend=0;
PORTB |= 0x10;//PB4(DF_CS)=1
delay(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//将第一页装入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay(1000);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B2_XFER);//将第二页装入buffer2
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay(1000);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_READ);//准备好从buffer1读取数据
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
//write_SPI(0x00);
state=1;//设置状态
TCCR0 = 0x00; //关闭定时/计数器0
uart0_init(); //UART初始化
temp=read_SPI(); //读取DataFlash
rosh=temp;
end();
number++;
if (DataFlashstate==0x01) //判断是否是下载熔丝位内容
{
UDR=temp; //将值赋给UDR
UCSRB |= 0x40; //UART发送中断使能
while (state==1) //等待中断发生
{
_CLI();
if (number==2)
{
rosl=temp;
}
else if (number==3) //下载完3个字节后跳出
{
lock=temp;
break;
}
_SEI();
}
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
UCSRB &= ~0x40; //关闭UART发送中断使能
TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
_SEI();
}
else if (DataFlashstate==0)
{
return;
}
else
{
UDR=temp; //将值赋给UDR
UCSRB |= 0x40; //UART发送中断使能
while (state==1); //进入循环,等待中断发生
//Programme_Chip();
}
PORTB |= 0x10;//PB4(DF_CS)=1
page=0;
delay1(10);
}
void DownToChip(void)
{
delay(2000);
PORTB |= 0x10; //PB4(DF_CS)=1
PORTB &= ~0x10; //PB4(DF_CS)=0
chip_unlock(); //片解锁
PORTB |= 0x10; //PB4(DF_CS)=1
delay(1000);
//~~~~~~~~~~~~~~~~~~~~~~~~~读DataFlash~~~~~~~~~~~~~~~~~~~~~~~~~~//
ts=0;
page=0;
number=0;
rxtxend=0;
PORTB |= 0x10;//PB4(DF_CS)=1
delay(1);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B1_XFER);//将第一页装入buffer1
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay(1000);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(MM_PAGE_TO_B2_XFER);//将第二页装入buffer2
write_SPI((uchar)(page>>7));
write_SPI((uchar)(page<<1));
write_SPI(0x00);
PORTB |= 0x10;//PB4(DF_CS)=1
page++;
delay(1000);
PORTB &= ~0x10;//PB4(DF_CS)=0
write_SPI(BUFFER_1_READ);//准备好从buffer1读取数据
write_SPI(0x00);
write_SPI(0x00);
write_SPI(0x00);
//write_SPI(0x00);
state=1;//设置状态
TCCR0 = 0x00; //关闭定时/计数器0
uart0_init(); //UART初始化
temp=read_SPI(); //读取DataFlash
rosh=temp;
number++;
ds=temp; //将值赋给ds
jud();
if (DataFlashstate==0x01) //判断是否是下载熔丝位内容
{
UDR=temp; //将值赋给UDR
UCSRB |= 0x40; //UART发送中断使能
while (state==1) //等待中断发生
{
_CLI();
if (number==2)
{
rosl=temp;
}
else if (number==3) //下载完3个字节后跳出
{
lock=temp;
break;
}
_SEI();
}
Write_ros(); //写熔丝位
PORTB |= 0x10; //PB4(DF_CS)=1
state =0; //状态清0
page=0;
upordown=0;
UCSRB &= ~0x40; //关闭UART发送中断使能
TCCR0 = 0x05; //启动定时/计数器0
temp2=proe;
temp3=pror;
_SEI();
}
else if (DataFlashstate==0)
{
return;
}
else
{
//UDR=temp; //将值赋给UDR
//UCSRB |= 0x40; //UART发送中断使能
//while (state==1); //进入循环,等待中断发生
Programme_Chip();
}
PORTB |= 0x10;//PB4(DF_CS)=1
page=0;
delay1(10);
}
void ReadFromChip(void)
{
if (y==0)
{
as=0x20;
IO_SPI();
as=adh;
IO_SPI();
as=adl;
IO_SPI();
as=0x00;
IO_SPI_READSP();
//ws[z]=rs;
/*
as=0x4C;
IO_SPI();
as=adh;
IO_SPI();
as=adl;
IO_SPI();
as=0x00;
IO_SPI();
*/
y=1;
if (adl==0xFF)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -