標(biāo)題: VB上位機(jī)與avr單片機(jī)18b20下位機(jī)程序 [打印本頁(yè)]

作者: 李好123    時(shí)間: 2020-11-18 11:42
標(biāo)題: VB上位機(jī)與avr單片機(jī)18b20下位機(jī)程序

程序:
#include <iom16v.h>                        //包含型號(hào)頭文件
#include <macros.h>                        //包含"位"操作頭文件
#include <stdio.h>
#include <DS18B20.h>
#define uchar unsigned char        
#define uint  unsigned int
#define SEGLK PA3        //數(shù)碼管段選鎖存器控制端
#define BITLK PA4        //數(shù)碼管位選鎖存器控制端
#define LEDLK PA2        //LED鎖存器控制端

const SEGMENT[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
                           0x7f,0x6f};
/*******************************************
函數(shù)名稱: Delayms
功    能: 延時(shí)指定毫秒(8M晶振)
參    數(shù): MS--延時(shí)的毫秒數(shù)
返回值  : 無(wú)
********************************************/
void Delayms(uint MS)                  
{
uint i,j;
for( i=0;i<MS;i++)
for(j=0;j<1141;j++);        //1141是在8MHz晶振下,通過(guò)軟件仿真反復(fù)實(shí)驗(yàn)得到的數(shù)值
}
/*******************************************
函數(shù)名稱: Board_init
功    能: 初始化實(shí)驗(yàn)板,關(guān)閉全部功能
參    數(shù): 無(wú)
返回值  : 無(wú)
/********************************************/
void Board_init(void)
{
        DDRA=0xFF;        //將四個(gè)IO口全部配置為輸出
        DDRB=0xFF;
        DDRC=0xFF;
        DDRD=0xFF;

        SPCR=0x00;        //還原SPI口,使其為正常端口        

        PORTA|=BIT(PA6);//(BEEP)關(guān)閉蜂鳴器
        PORTC&=~BIT(PC6);          //(RELAY)初始化繼電器為常閉連接狀態(tài)        
        PORTA&=~BIT(PA1);        //關(guān)閉SPI總線DA
        PORTB=0xFF;        //關(guān)閉LED
        PORTA|=BIT(LEDLK);        //鎖存數(shù)據(jù),使LED熄滅
         Delayus(5);               
         PORTA&=~BIT(LEDLK);
        
        PORTB=0x00;           //輸出段選
         PORTA|=BIT(SEGLK);                  
         Delayus(50);                          
         PORTA&=~BIT(SEGLK);            
         
         PORTB=0xFF;           //輸出位選
         PORTA|=BIT(BITLK);                  
         Delayus(50);                                   
         PORTA&=~BIT(BITLK);           
        
        PORTD=0xFF;
        PORTA|=BIT(PA7);
        Delayus(50);
        PORTA&=~BIT(PA7);

        
}

/*******************************************
函數(shù)名稱: One_smg_display
功    能: 指定的數(shù)碼管顯示指定的內(nèi)容
參    數(shù): data--顯示的內(nèi)容(0-15)
                    number--指定的數(shù)碼管(1-6)
返回值  : 無(wú)
/********************************************/
void One_smg_display(uchar data,uchar number)
{
PORTB|=0x3F;                           //輸出位選
PORTA|=BIT(BITLK);                   //更新位選
Delayus(50);                           //調(diào)整時(shí)序
PORTA&=~BIT(BITLK);           //鎖存位選

PORTB=SEGMENT[data];           //輸出段選
PORTA|=BIT(SEGLK);                   //更新段選
Delayus(50);                           //調(diào)整時(shí)序
PORTA&=~BIT(SEGLK);           //鎖存段選

number=~BIT(number-1);           //調(diào)整位選(指定時(shí)是1~6,而操作的時(shí)候是0~5)
PORTB=number;                           //輸出位選
PORTA|=BIT(BITLK);                   //更新位選
Delayus(50);                                   //調(diào)整時(shí)序
PORTA&=~BIT(BITLK);           //鎖存位選
}

/********************************************/
void uart0_init(void)
{
  UCSRB = 0x00;  //禁止發(fā)送
  UCSRA = 0x02;  //倍速
  UCSRC = 0x06;  //8位數(shù)據(jù)位,1個(gè)停止位
  UBRRL = 0x67;  //波特率為9600
  UBRRH = 0x00;
  UCSRB = 0x18;  //允許發(fā)送
}
/********************************************************/
void uart0_send(uchar i)  //發(fā)送一個(gè)字符
{
  while(!(UCSRA&(1<<UDRE))); //等待發(fā)送緩沖區(qū)為空
    UDR=i;                    //發(fā)送數(shù)據(jù)
  while(!(UCSRA&(1<<TXC)));                 //等待發(fā)送完畢
    UCSRA|=1<<TXC;                   //清除發(fā)送完畢狀態(tài)位
}
/*******************************************/
void str_send(char *s) //發(fā)送一串字符
{
while(*s)
{
  uart0_send(*s); //字符串未結(jié)束則繼續(xù)發(fā)送
  s++;
}
}
/*********************************************/
uchar uart0_receive(void) //接收一個(gè)字符
{
  while(!(UCSRA&(1<<RXC)));//等待接收數(shù)據(jù)
  return UDR;               //返回?cái)?shù)據(jù)
}

/*******************************************
函數(shù)名稱: main
功    能: 6個(gè)數(shù)碼管循環(huán)滾動(dòng)顯示指定的內(nèi)容
參    數(shù): 無(wú)
返回值  : 無(wú)
/********************************************/
void main(void)
{         uint t=0;
     uchar temh_d,teml_d,temm_d;
         //uchar RDATA[3];
         uart0_init();
         Board_init();           //初始化開(kāi)發(fā)板
         while(1)
         {
         
          t=Read_temp()*0.625;//得到轉(zhuǎn)換溫度值
      Delayms(2);
          temh_d=t/100;
          temm_d=(t/10)%10;
          teml_d=t%10;
         
           
                //uart0_send(temh_d+48);
                //Delayms(1);
           // uart0_send(temm_d+48);
                //Delayms(1);
            uart0_send(temh_d+48);
                uart0_send(temm_d+48);
                str_send(".");
                uart0_send(teml_d+48);
                uart0_send(32);
                uart0_send(32);
                uart0_send(32);
                //Delayms(1);
                //str_send(" ");
               
          One_smg_display(teml_d,6);
          Delayms(2);
          One_smg_display(temm_d,5);
          Delayms(2);
          One_smg_display(temh_d,4);
          Delayms(2);
        
         }
}

VB上位機(jī)與18b20下位機(jī).zip

32.79 KB, 下載次數(shù): 22, 下載積分: 黑幣 -5


作者: 51hei團(tuán)團(tuán)    時(shí)間: 2020-11-29 03:18
好資料,51黑有你更精彩!!!




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1