⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 超声波测距仪套件 电路图、GCC源代码资料
💻 C
字号:
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "MAIN.h"
#include "MISC.h"
#include "TWI.h"
#include "3310LCD.h"
#include "picture.h"

volatile unsigned char MODE_ST=0;
volatile unsigned char RUN_ST =0;
volatile unsigned char CAP_ST =0;
volatile unsigned int  CAPTIME=0;
volatile unsigned char CAPCNT =0;
volatile unsigned char GAIN   =0;
volatile unsigned char AGAIN  =0;
volatile unsigned char BLANK  =0;

unsigned char DistanceS[]="Dist:  xxxxxmm";	//7
unsigned char     VBATS[]="Bat:    xxxxmV";	//8
unsigned char     TEMPS[]="Temp: xxx.xx'c";	//6+10
unsigned char     MODES[]="MODE=x ";			//5
unsigned char    STATUS[]="GAIN=xxx xxxms";	//5+9

unsigned int  VBAT,VKEY;
unsigned char TEMP[2];

void init_devices(void)
{
    asm volatile("cli"::); 				//disable all interrupts
    PORTA =(unsigned char) ~((1<<ADCBAT)|(1<<ADCKEY)|(1<<ADCSIG));
    DDRA  = 0x00;
    PORTB = 0xFF;
    DDRB  =(1<<LCD_nRST)|(1<<LCD_DnC)|(1<<LCD_nCE)|(1<<SPI_MOSI)|(1<<SPI_CLK)|(1<<TX_nOC0);
    PORTC = 0xFF;
    DDRC  = 0x00;
    PORTD = 0xFF;
    DDRD  = (1<<TXD)|(1<<RX_nPOWER)|(1<<DP_nCS);


    //UART0 initialize
    // desired baud rate: 9600
    // actual: baud rate:9615 (0.2%)
    // char size: 8 bit
    // parity: Disabled
    UCSRB = 0x00; 						//disable while setting baud rate
    UCSRA = 0x00;
    UCSRC = (1<<URSEL)|0x06;
    UBRRL = 0x33; 						//set baud rate lo
    UBRRH = 0x00; 						//set baud rate hi
    UCSRB = 0x98;

    ADCSRA=0x86;
    ADMUX=0xc0;

    TWBR = 73;
    SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);// 使能SPI接口,主机模式,500KHz时钟

    MCUCR= (1<<ISC11);					//INT0电平,INT1下降沿
    msDelay(100);
    GIFR = 0xFF;
    TIFR = 0xFF;
    TIMSK= (1<<TOIE1); 					//T1OVF
    asm volatile("sei"::);   			//re-enable interrupts
    //all peripherals are now initialized
}


int main (void)
{
    unsigned char c;
    init_devices();
    PORTD &=~(1<<RX_nPOWER);			//POWER ON
    IIC_SLA=DS1721_ADDR;
    c=actTMPCFG;
    rtc_write(CMD_CONFIG,&c,0);
    DP_DEC(255);
    DP_DEC(1);							//min GAIN

    uart_puts("Ultra Sonic Finder");
    uart_putchar(0x0D);
    uart_putchar(0x0A);
    LCD_init(); 						//初始化液晶
    LCD_draw_Screen(AVRlogo);
    msDelay(500);
    LCD_draw_Screen(Ulogo);
    XP();
    while(1);
}

void XP(void)
{
    unsigned int i=0;
    unsigned int captemp=0;
    unsigned char capok=0,capcnt=0;
    rtc_write(CMD_STARTT,0,0);
    GICR = 0xC0;
    msDelay(1000);
    GICR = 0x00;
    PORTB &=~(1<<LCD_nRST);      		//LCD复位(降低干扰)
    msDelay(1);
    switch (MODE_ST)
    {
    case 0:
        capok=0;
        while (capok<3)
        {
            get_DT();
            if ((captemp<=(CAPTIME+CAPTIME/100+40)) && (captemp>=(CAPTIME-CAPTIME/100-40)))
                capok++;
            else
                capok=0;
            captemp=CAPTIME;
            capcnt++;
            if (capcnt>10)
                break; //超时退出
        }
        break;
    case 1:
        get_FIX();
        break;
    }
    i=Calc_Distance();
    HEX2toASC5(i,&DistanceS[7],5,HIDE);
    VBAT=read_adc(ADCBAT);
    VBAT=(VBAT*10)/4*2;					//mV  2R
    HEX2toASC5(VBAT,&VBATS[8],4,HIDE);
    rtc_read(CMD_READT,&TEMP[0],2);
    HEX2toASC5(TEMP[0],&TEMPS[6],3,HIDE);
    i= (TEMP[1]>>6)*25;
    HEX2toASC5(i,&TEMPS[10],2,NOHIDE);
    MODES[5]=MODE_ST+0x30;
    if (MODE_ST==0)
    {
        HEX2toASC5(AGAIN,&STATUS[5],3,HIDE);
    }
    else
    {
        HEX2toASC5( GAIN,&STATUS[5],3,HIDE);
    }
    HEX2toASC5(BLANK,&STATUS[9],3,HIDE);
    PORTD &=~(1<<RX_nPOWER);			//POWER ON
    LCD_init(); 						//初始化液晶
    if (VBAT>4000)
    {
        LCD_write_byte(0x21, 0);		// 使用扩展命令设置LCD模式
        LCD_write_byte(0x80+0x48, 0);	// 设置偏置电压
        LCD_write_byte(0x20, 0);  		// 使用基本命令
    }
    LCD_write_english_string(0,0,MODES);
    LCD_write_english_string(0,1,DistanceS);
    LCD_write_english_string(0,2,TEMPS);
    LCD_write_english_string(0,3,VBATS);
    LCD_write_english_string(0,4,STATUS);
    LCD_write_english_string(0,5,"www.OurAVR.com");
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -