?? 四路熱敏電阻溫度采樣程序
字號:
//ICC-AVR application builder : 2006-11-14 10:39:01
// Target : M16
// Crystal: 4.0000Mhz
#include <iom16v.h>
#include <macros.h>
#define vfd_data_port PORTB
#define vfd_data_ddr DDRB
#define vfd_busy_pin PINB
#define vfd_busy_ddr DDRB
#define vfd_control_port PORTD
#define vfd_control_ddr DDRD
#define vfd_RS 0x20 //PORTD^5
#define vfd_EN 0x40 //PORTD^6
#define vfd_RW 0x80 //PORTD^7
//#define vfd_RW 0x40 //PORTD^1
//#define vfd_EN 0x80 //PORTD^2
#define busy 0x80 //VFD_DB7
struct sem
{
unsigned int ad_samp; //a/d sampling data
unsigned int pc_trans; //pc transfer to chip
unsigned char class_addr[4]; //data addr
unsigned char class_data[7]; //asc ii code from pc to chip or chip to pc
}dypass[9];
unsigned char adc_mux,i;
unsigned int adc_rel,adc_old,sin;
unsigned char adc_meas[20];
unsigned char hex_meas[5];
unsigned char rece_coun;
unsigned char meas_coun;
unsigned char *point;
unsigned char channel;//sampling channel
/*----------------------------------函數聲明------------------------------------------*/
void vfd_init(void);
void vfd_write_command(unsigned char command,unsigned char wait_en);
void vfd_write_data(unsigned char char_data);
void wait_enable(void);
void display_a_char(unsigned char position,unsigned char char_data);
void d_a_s(unsigned char col,unsigned char *ptr);
void display_a_ch_r(unsigned char position,unsigned char char_data);
void delay_1ms(void);
void delay_nms(unsigned int n);
void adc_init(void);
void filter(unsigned char maj);
void hex_asc(unsigned int dt_meas,unsigned char *g);
//顯示的兩行字符
unsigned char str1[]="NO1: ℃ NO2: T";
unsigned char str2[]="NO3: T NO4: T";
const unsigned char t_per[255]={
0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,0x99,//0~9
0x99,0x99,0x99,0x99,0x98,0x97,0x96,0x95,0x94,0x93,//10~19
0x92,0x90,0x89,0x88,0x86,0x85,0x84,0x83,0x82,0x81,//20~29
0x80,0x78,0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,//30~39
0x70,0x69,0x68,0x67,0x66,0x65,0x64,0x63,0x62,0x61,//40~49
0x60,0x59,0x58,0x58,0x57,0x57,0x56,0x56,0x55,0x55,//50~59
0x54,0x53,0x53,0x52,0x51,0x50,0x50,0x49,0x49,0x48,//60~69
0x48,0x48,0x47,0x47,0x46,0x46,0x45,0x45,0x44,0x44,//70~79
0x44,0x43,0x43,0x43,0x42,0x42,0x41,0x41,0x40,0x40,//80~89
0x40,0x39,0x39,0x38,0x38,0x37,0x37,0x37,0x36,0x36,//90~99
0x35,0x35,0x35,0x34,0x34,0x33,0x33,0x33,0x32,0x32,//100~109
0x32,0x31,0x31,0x30,0x30,0x30,0x29,0x29,0x28,0x28,//110~119
0x27,0x27,0x27,0x27,0x26,0x26,0x26,0x25,0x25,0x24,//120~129
0x24,0x24,0x23,0x23,0x23,0x22,0x22,0x22,0x21,0x21,//130~139
0x21,0x20,0x20,0x20,0x19,0x19,0x19,0x18,0x18,0x18,//140~149
0x17,0x17,0x16,0x16,0x16,0x15,0x15,0x15,0x14,0x14,//150~159
0x14,0x13,0x13,0x13,0x12,0x12,0x12,0x11,0x11,0x11,//160~169
0x10,0x10,0x9,0x9,0x9,0x8,0x8,0x7,0x7,0x6,//170~179
0x6,0x6,0x5,0x5,0x5,0x4,0x4,0x3,0x3,0x2,//180~189
0x2,0x1,0x1,0x0,0x0,0x0,0x0,0x1,0x1,0x2,//190~199
0x2,0x3,0x3,0x4,0x4,0x3,0x5,0x5,0x5,0x6,//200~209
0x7,0x8,0x9,0x9,0x10,0x10,0x11,0x11,0x12,0x12,//210~219
0x13,0x13,0x14,0x14,0x15,0x15,0x16,0x16,0x17,0x17,//220~229
0x18,0x18,0x19,0x20,0x21,0x22,0x23,0x24,0x25,0x26,//230~239
0x27,0x28,0x29,0x30,0x31,0x32,0x33,0x34,0x35,0x36,//240~249
0x37,0x38,0x39,0x40,0x50,};//250~254
/*------------------------------------主函數-------------------------------------------*/
//在VFD上顯示hello和www.xmbyte.com兩行字符
void main(void)
{
adc_init();
vfd_data_port=0xff;
vfd_data_ddr=0xff;
vfd_control_port=0xF0;
vfd_control_ddr=0xF0;
DDRC = 0xFF;
PORTC = 0xff; //m103 output only
vfd_init();
//d_a_s(0,str1);
//d_a_s(1,str2);
while(1)
{
PORTC=0XFF;
vfd_write_command(0xf,1);
vfd_write_command(0x6,1);
d_a_s(0,str1);
//A/D 采樣四個通道
filter(0);
filter(1);
filter(2);
filter(3);
sin=4;
for(i=2;i<=3;i++)
{
str1[sin]=dypass[0].class_data[i];
sin++;
}
sin=13;
for(i=2;i<=3;i++)
{
str1[sin]=dypass[1].class_data[i];
sin++;
}
sin=4;
for(i=2;i<=3;i++)
{
str2[sin]=dypass[2].class_data[i];
sin++;
}
sin=13;
for(i=2;i<=3;i++)
{
str2[sin]=dypass[3].class_data[i];
sin++;
}
d_a_s(0,str1);
d_a_s(1,str2);
delay_nms(1000);
PORTC=0X00;
delay_nms(1000);
d_a_s(1,str2);
}
}
void adc_init(void)
{
DDRA=0x00;
PORTA=0x00;
ADCSRA = 0x00;
ADMUX =(1<<REFS0)|(adc_mux&0x0f);//選擇內部AVCC為基準
ACSR =(1<<ACD);//關閉模擬比較器
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1) ;//64分頻
}
//將無符號的整形數轉換為asc碼,保存于指針g開始的連續四個空間
void hex_asc(unsigned int dt_meas,unsigned char *g)
{
*g=(unsigned char)(dt_meas/0x1000);
if(*g<10)
*g+=0x30;
else *g+=0x37;
g++;
*g=(unsigned char)((dt_meas/0x100)%0x10);
if(*g<10)
*g+=0x30;
else *g+=0x37;
g++;
*g=(dt_meas%0x100)/0x10;
if(*g<10)
*g+=0x30;
else *g+=0x37;
g++;
*g=(dt_meas%0x100)%0x10;
if(*g<10)
*g+=0x30;
else *g+=0x37;
}
void filter(unsigned char maj)
{ unsigned char *q,y,wendu;
unsigned int ad_temp[18],phillp=0;
if(maj!=8)
{
ADMUX=0x40+maj;
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1) ;//64分頻
for(y=0;y<=16;y++)
{ ADCSRA |= 0x40; // Start a new A/D conversion
while (!(ADCSRA & (1<<ADIF))); // wait until ADC is ready
ad_temp[y]=ADC&0X3FF;
}
for(y=6;y<=13;y++)
phillp+=ad_temp[y];
adc_rel=phillp/32;
y=adc_rel;
wendu=t_per[y];
//y=dypass[maj].ad_samp;
}
else {
channel=PORTD;
adc_rel=(unsigned int)channel;
dypass[maj].ad_samp=adc_rel;
}
// dypass[maj].class_data[0]=" ";
//dypass[maj].class_data[1]=" ";
dypass[maj].class_data[2]=0x30+wendu/16;
dypass[maj].class_data[3]=0x30+wendu%16;
}
/*------------------------------------液晶驅動函數-------------------------------------*/
//液晶初始化
void vfd_init(void)
{
delay_nms(15);
vfd_write_command(0x38,0);//顯示模式設置三次(此時不管vfd空閑與否)
delay_nms(5);
vfd_write_command(0x38,0);
delay_nms(5);
vfd_write_command(0x38,0);
delay_nms(5);
vfd_write_command(0x38,1);//顯示模式設置(從此之后均需vfd空閑)
vfd_write_command(0x08,1);//顯示關閉
vfd_write_command(0x01,1);//顯示清屏
vfd_write_command(0x06,1);//顯示光標移動設置
vfd_write_command(0x0c,1);//顯示開及光標設置
}
//寫指令函數: E=高脈沖 RS=0 RW=0
void vfd_write_command(unsigned char command,unsigned char wait_en)//command為指令,wait_en指定是否要檢測VFD忙信號
{
if(wait_en)wait_enable();//若wait_en為1,則要檢測VFD忙信號,等待其空閑
vfd_control_port&=~vfd_RS;//RS=0
vfd_control_port&=~vfd_RW;//RW=0
vfd_control_port&=~vfd_EN;//E=0,下面給VFD一個高脈沖
NOP();
vfd_control_port|=vfd_EN;//E=1
vfd_data_port=command;
vfd_control_port&=~vfd_EN;//重設E=0
}
//寫數據函數: E =高脈沖 RS=1 RW=0
void vfd_write_data(unsigned char char_data)
{
wait_enable();//等待VFD空閑
vfd_control_port|=vfd_RS;//RS=1
vfd_control_port&=~vfd_RW;//RW=0
vfd_control_port&=~vfd_EN;//E=0,下面給VFD一個高脈沖
NOP();
vfd_control_port|=vfd_EN;//E=1
vfd_data_port=char_data;
vfd_control_port&=~vfd_EN;//重設E=0
}
//正常讀寫操作之前必須檢測VFD控制器狀態:E=1 RS=0 RW=1;DB7: 0 VFD控制器空閑,1 VFD控制器忙。
//檢測忙信號,等待VFD空閑函數
void wait_enable(void)
{
//delay_nms(10);
vfd_busy_ddr&=~busy;//設置busy口為輸入
vfd_control_port&=~vfd_RS;//RS=0
vfd_control_port|=vfd_RW;//RW=1
NOP();
vfd_control_port|=vfd_EN;//E=1
while(vfd_busy_pin&busy);//等待VFD_DB7為0
vfd_control_port&=~vfd_EN;//重設E=0
vfd_busy_ddr|=busy;//設置busy口為輸出
}
//指定位置顯示一個字符:第一行位置0~15,第二行16~31
//顯示一個字符函數
void display_a_char(unsigned char position,unsigned char char_data)//參數position指定位置0~31,char_data為要顯示的字符
{
unsigned char position_tem;
if(position>=0x10)
position_tem=position+0xb0;
else
position_tem=position+0x80;
vfd_write_command(position_tem,1);
vfd_write_data(char_data);
}
//指定一行顯示連續字符串:0顯示在第一行,1顯示在第二行,注字符串不能長于16個字符
//顯示一行連續字符串函數
void d_a_s(unsigned char col,unsigned char *ptr)//參數col指定行,*ptr指字符串數組的首指針
{
unsigned char col_tem,i;
col_tem=col<<4;//若col為1(即在VFD第二行顯示字符串),先把col左移4位,使顯示字符的首位置改到第二行首位,即位置16
for(i=0;i<16;i++)
display_a_char(col_tem++,*(ptr+i));
}
//光標定位子程序
void display_a_ch_r(unsigned char position,unsigned char char_data)//參數position指定位置0~31,char_data為要顯示的字符
{
unsigned char position_tem;
if(position>=0x10)
position_tem=position+0xb0;
else
position_tem=position+0x80;
vfd_write_command(position_tem,1);
vfd_write_data(char_data);
vfd_write_command(position_tem,1);
}
/*---------------------------------------延時函數-------------------------------------*/
//1ms延時函數
void delay_1ms(void)
{
unsigned int i;
for(i=0;i<600;i++);
}
//n ms延時函數
void delay_nms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)delay_1ms();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -