📄 htd30v02.c
字号:
}
void delay_50us(void)
{
uchar i;
for(i=0;i<12;i++)
nop5();
}
//让FLASH进入读待命状态
void flash_reset_read(void)
{
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xf0);
}
//wait flash status reg
uchar read_flash_srd(void)
{
uchar temp1;
//first cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//second cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//third cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x70);
//forth cycle
temp1=read_flash_byte();
temp1&=0xf8;
return(temp1);
}
void clear_srd(void)
{
//first cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//second cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//third cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x50);
}
//erase chip
bit erase_flash(void)
{
uchar i;
i=3;
while(i--)
{
//first cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//second cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//third cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x80);
//forth cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//fifth cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//sixth cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x10);
delay_100us();
if(read_flash_srd()==0x80)
return(0);
}
clear_srd();
return(1);
}
//erase sector
bit erase_sector(uchar sector_add)
{
uchar i;
i=3;
while(i--)
{
//first cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//second cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//third cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x80);
//forth cycle
flash_high_add=0;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0xaa);
//fifth cycle
flash_high_add=0;
flash_mid_add=0x55;
flash_low_add=0x54;
write_flash_byte(0x55);
//sixth cycle
flash_high_add=sector_add*2;
flash_mid_add=0xaa;
flash_low_add=0xaa;
write_flash_byte(0x30);
delay_100us();
if(read_flash_srd()==0x80)
return(0);
}
clear_srd();
return(1);
}
//write flash page 1
//void write_page(void)
// {
// uchar i,k;
// data union
// {
// uchar target_byte[2];
// uint target_word;
// }address_flash;
// EA=0;
// flash_high_add=0;
// flash_mid_add=0xaa;
// flash_low_add=0xaa;
// write_flash_byte(0xaa);
// flash_high_add=0;
// flash_mid_add=0x55;
// flash_low_add=0x54;
// write_flash_byte(0x55);
// flash_high_add=0;
// flash_mid_add=0xaa;
// flash_low_add=0xaa;
// write_flash_byte(0xa0);
// flash_high_add=flash_address.flash_byte.page_MSB/2;
// flash_mid_add=(flash_address.flash_byte.page_MSB<<7)+(flash_address.flash_byte.page_LSB/2);
// flash_low_add=(flash_address.flash_byte.page_LSB<<7);
// SEL_OTHERS=1;
// mirror_377&=0xe0;
// mirror_377+=flash_high_add;
// sel_hc377=mirror_377;
// SEL_OTHERS=0;
// address_flash.target_byte[0]=flash_mid_add;
// address_flash.target_byte[1]=flash_low_add;
// for(i=0;i<128;i++)
// {
// k=flash_buf[i];
// set_extram;
// XBYTE[address_flash.target_word]=k;
// clr_extram;
// address_flash.target_byte[1]++;
// }
// delay_100us();
// while(read_flash_srd()!=0x80)
// {
// }
// EA=1;
// }
//////void flash_error1(void)
////// {
////// while(1) //send message to display
////// {
////// }
////// }
//void read_page(void) //将flash_address.flash_page地址的整个页面读到flash_buf
// {
// uchar i;
// uchar temp1;
// data union
// {
// uchar target_byte[2];
// uint target_word;
// }address_flash;
//// clear_srd();
////read page0 of flash
// while(read_flash_srd()!=0x80)
// {
// }
// flash_high_add=0;
// flash_mid_add=0xaa;
// flash_low_add=0xaa;
// write_flash_byte(0xaa);
// flash_high_add=0;
// flash_mid_add=0x55;
// flash_low_add=0x54;
// write_flash_byte(0x55);
// flash_high_add=0;
// flash_mid_add=0xaa;
// flash_low_add=0xaa;
// write_flash_byte(0xf0);
// flash_high_add=flash_address.flash_byte.page_MSB/2;
// flash_mid_add=(flash_address.flash_byte.page_MSB<<7)+(flash_address.flash_byte.page_LSB/2);
// flash_low_add=(flash_address.flash_byte.page_LSB<<7);
// address_flash.target_byte[0]=flash_mid_add;
// address_flash.target_byte[1]=flash_low_add;
// SEL_OTHERS=1;
// mirror_377&=0xe0;
// mirror_377+=flash_high_add;
// sel_hc377=mirror_377;
// SEL_OTHERS=0;
// for(i=0;i<128;i++)
// {
// set_extram;
// temp1=XBYTE[address_flash.target_word];
// clr_extram;
// flash_buf[i]=temp1;
// address_flash.target_byte[1]++;
// }
// }
void uart_same_way(void)
{
uart_sendhead++;
send_ok=0;
// TI=1;
ACC=com1_send_buf[0];
TB8=(~P); //清时间,定时发送数据
SBUF=com1_send_buf[0];
uart_sendtail++;
while(send_ok==0)
{
}
}
void make_checksum1(void)
{
uchar i;
for(i=0;i<uart_sendhead;i++)
checksum_send^=com1_send_buf[i];
}
void start_checksum1(void)
{
checksum_send=0;
make_checksum1();
}
void return_uart(void)
{
com1_send_buf[0]=0x55;
com1_send_buf[1]=0x7a;
com1_send_buf[2]=order_name;
com1_send_buf[5]=0xff;
}
void make_checksum_send(void)
{
start_checksum1();
com1_send_buf[uart_sendhead]=checksum_send;
uart_same_way();
}
void uart_same_response(void)
{
com1_send_buf[3]=0x00;
return_uart();
make_checksum_send();
}
void uart_response_right(void)
{
com1_send_buf[4]=0x00;
uart_sendhead=6;
uart_same_response();
}
void calculate_distance(void) //计算当前的累计里程数,HEX结果存放在distance.distance_long中
{
add_start.add_word=LAST_DISTANCE; //取出最后上电当时的里程示数
fm_read(distance.distance_byte,4);
distance.distance_long+=(day_pulse*20+1)/km_pulse.km_word/2; //求出现在的里程并保存
add_start.add_word=NOW_DISTANCE;
fm_write(distance.distance_byte,4);
}
void convert_long(void) //将tempe.temp_long 的HEX里程数转化为BCD格式的存放在EEP_BUF中
{
uint word_shang,word_yu;
uchar byte_shang_shang,byte_shang_yu,byte_yu_shang,byte_yu_yu;
// tempe.temp_long=distance.distance_long;
word_shang=tempe.temp_long/10000;
word_yu=tempe.temp_long%10000;
byte_shang_shang=word_shang/100;
byte_shang_yu=word_shang%100;
byte_yu_shang=word_yu/100;
byte_yu_yu=word_yu%100;
eep_buf[0]=byte_shang_shang/10;
eep_buf[1]=byte_shang_shang%10;
eep_buf[2]=byte_shang_yu/10;
eep_buf[3]=byte_shang_yu%10;
eep_buf[4]=byte_yu_shang/10;
eep_buf[5]=byte_yu_shang%10;
eep_buf[6]=byte_yu_yu/10;
eep_buf[7]=byte_yu_yu%10;
}
uchar test_symbol(uint address_symbol)
{
uchar temp_a;
add_start.add_word=address_symbol;
fm_read(&temp_a,1);
return(temp_a==0xaa);
}
void send1_without_checksum(void)
{
start_checksum1();
uart_sendhead--;
uart_same_way();
}
//////////////////////////////////////////////////////////////////////
//函数名:
//创建者:templa
//创建时间:2005年03月16日05时45分
//函数体中要使用的全局变量:head_local/head_tail/hour_number/flash_address
//函数体中要使用的临时变量:temp_local
//函数功能:得到要被传送的数据的第一个页面的位置,如果头在EEP中head_local=0,在FLASH中,head_local=1,
//而head_tail的作用表示是在页面的前部还是后部,=1为后部/=0为前部
void judge_local_hour(void)
{
uint temp_local;
uchar i;
//如果当前小时已经有分钟速度了就需要将其作为最后的数据
head_local=0;
temp_local=hour_number;
add_start.add_word=HOUR_MIN_PTR_A;
fm_read(&tempa,1);
head_tail=0;
head_tail1=0;
if(tempa>=60)
{
head_tail=1;
add_start.add_word=HOUR_TIME2;
add_start.add_word+=4;
fm_read(flash_buf,60);
for(i=0;i<60;i++)
{
if(flash_buf[i]!=0)
{
head_tail1=1;
if(head_tail1==1)
break;
}
}
}
if(have_speed) //当前小时的数据有效
{
temp_local--;
if(temp_local==0) //如果就要这一则,找到了位置
return;
//如果要的部分还未找到,继续找
if(head_tail)
{
head_tail=0; //检查EEP的前部是否是要找的头部位置
temp_local--;
if(temp_local==0) //如果就要这一则,找到了位置
return;
}
//要传输的数据头一定在FLASH中
//如果仍然没有找到头,要到FLASH中去查
head_local=1;
head_tail=(temp_local%2);
add_start.add_word=PAGE_360H_OFF_TAIL;
fm_read(tempd.temp_byte,2);
flash_address.flash_page=tempd.temp_word; //算出绝对页面地址
if((flash_address.flash_page-0x3800)*2>=temp_local)
flash_address.flash_page-=((temp_local+1)/2);
//数据将轮回到上一个扇区
else
flash_address.flash_page=(flash_address.flash_page-((temp_local+1)/2)+0x800);
flash_address.flash_page&=0x3fff;
}
else
{
if(head_tail)
{
head_tail=0; //检查EEP的前部是否是要找的头部位置
temp_local--;
if(temp_local==0) //如果就要这一则,找到了位置
return;
}
//如果仍然没有找到头,要到FLASH中去查
head_local=1;
head_tail=(temp_local%2);
add_start.add_word=PAGE_360H_OFF_TAIL;
fm_read(tempd.temp_byte,2);
flash_address.flash_page=tempd.temp_word; //算出绝对页面地址
if((flash_address.flash_page-0x3800)*2>=temp_local)
flash_address.flash_page-=((temp_local+1)/2);
//数据将轮回到上一个扇区
else
flash_address.flash_page=(flash_address.flash_page-((temp_local+1)/2)+0x800);
flash_address.flash_page&=0x3fff;
}
}
//函数judge_local_hour结束
//*********************************************************************
void caculate_sum_send(uchar send_number) //caculate checksum with old sum and send it without checksum
{
uart_sendhead=send_number;
make_checksum1();
uart_sendhead--;
uart_same_way();
}
void send_checksum1(void)
{
com1_send_buf[0]=checksum_send;
uart_sendhead=0;
uart_same_way();
}
void send_page_head(void)
{
copy_ram_same(flash_buf,com1_send_buf,4);
com1_send_buf[4]=0;
caculate_sum_send(5);
copy_ram_same(flash_buf+4,com1_send_buf,60);
caculate_sum_send(60);
}
void send_page_tail(void)
{
copy_ram_same(flash_buf+64,com1_send_buf,4);
com1_send_buf[4]=0;
caculate_sum_send(5);
copy_ram_same(flash_buf+68,com1_send_buf,60);
caculate_sum_send(60);
}
void send_eep_head(void)
{
add_start.add_word=HOUR_TIME1;
fm_read(com1_send_buf,4);
com1_send_buf[4]=0;
caculate_sum_send(5);
add_start.add_word=HOUR_SPEED1;
fm_read(com1_send_buf,60);
caculate_sum_send(60);
}
void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -