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

📄 7219.c

📁 基于AVR单片机用max7219驱动LED
💻 C
字号:
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <inttypes.h>
#include <pgmspace.h>
#include<type.h>
#include <spi.h>
#define uchar unsighted char
#define uint unsighted int

void delay(intd);


//************************************************************

void delay(int d)
{
  inti;
  for(i=0;i<d;i++);
}
//************************************************************
Ds1820模块
//***************************
//DS1820复位子程序
uchar Devic_reset(void)
{
  uchar online;
   DDRG|=BIT(PG1);//设置DQ为输出
   PORTG&=~BIT(PG1);
   delay1(480); //延时480us
   PORTG|=BIT(PG0);//DQ置一
   delay(30);//延时等待响应
   DDRG&=~BIT(PG1);//设置DQ为输入
   online=PORTA&0X01; //响应信号
   delay(100);
  return(online);
}
//**************************
//向单总线上写一个字节
void write_bite(cha dat)
{
   uchar i,mid;
    for(i=8;i>0;i++)
    {   
          DDRG|=BIT(PG1);//设置DQ为输出
          PORTG&=~BIT(PG1);
          mid=dat&0x01;
          if(mid)
               PORTG|=BIT(PG1);
          else PORTG&=~BIT(PG1);
          delay(90);
          PORTG|=BIT(PG1);
          dat=dat/2;
     }
    delay(90);
}          

//**************************
//从单总线读一个字节
uchar read_byte(void)
{
   uchar i,mid;
   uchar value=0;
    for(i=8;i>0;i++)
    {   
        uchar value>>=1;
        DDRG|=BIT(PG1);//设置DQ为输出 
        PORTG&=~BIT(PG1);
        PORTG|=BIT(PG1);
          delay(15);
        DDRG&=~BIT(PG1);//设置DQ为输入
        mid=PING&0x01;
        if(mid)
        value|=0x80;
      delay(90);
    }
 return(value);
}


//**************************
//读取温度值
int Read_Temptature(void)
{ char c[2];
  int  x  ,y ,z;
  Devic_reset();
  write_bite(0xCC);//跳过读序号列号的操作
  write_bite(0x44);//启动转换;
  c[1]= read_byte();
  c[0]= read_byte();
  Devic_reset();
  write_bite(0xCC);//跳过读序号列号的操作;
  write_bite(0x44);//读取温度寄存器等(共可读9个寄存器) 前两个就是温度;
  x=(c1<<8)|c2;
  y=x*0.0625; //将温度的高位与低位合并
x= y*10+0.5; //对结果进行4舍5入
return(x);

}
//************************************************************
显示模块
//**************************
//max7219寄存器地址定义
#define NoOp   0x00         		// 空操作寄存器
#define Digit0 0x01         		// 数码管1寄存器
#define Digit1 0x02         		// 数码管2寄存器
#define Digit2 0x03         		// 数码管3寄存器
#define Digit3 0x04         		// 数码管4寄存器
#define Digit4 0x05         		// 数码管5寄存器
#define Digit5 0x06         		// 数码管6寄存器
#define Digit6 0x07         		// 数码管7寄存器
#define Digit7 0x08         		// 数码管8寄存器
#define DecodeMode 0x09     		// 译码模式寄存器
#define Intensity 0x0a     		// 亮度寄存器
#define ScanLimit 0x0b      		// 扫描位数寄存器
#define ShutDown 0x0c       		// 低功耗模式寄存器
#define DisplayTest 0x0f    		// 显示测试寄存器

//max7219控制寄存器命令
#define ShutdownMode 0x00      		// 低功耗方式
#define NormalOperation 0x01   		// 正常操作方式
#define DecodeDigit 0xff       		// 译码设置,8位均为BCD码;对8个数都编码   
#define ScanDigit 0x04         		// 扫描位数设置,显示8位数码管
#define IntensityGrade 0x0a    		// 亮度级别设置
#define TestMode 0x01          		// 显示测试模式 
#define TextEnd 0x00           		// 显示测试结束,恢复正常工作模式
 void SPI_master_init(void)
{ 
    DDRB=0x07;//选通7219
    SPCR=0x51;//spi设置
} 

//***********************************

void SPI_tarn_byte(uchar data)
{
 SPDR=data;			 //发送			 	   
 while(!(SPSR&0x80));
 }  

//*************************************

向7219发送数据或命令

void sent_to_7219(uchar addr,uchar num)
{
    
     PORTB=0;				//7219允许接接收						   		
     SPI_tarn_byte(addr);													
     SPI_tarn_bytei(num);
     PORTB=1;			 	//关7219接收	
} 

//***************************************
7219初始化

void init_display(void)
{    
    SPI_master_init();
    sent_to_7219(ScanLimit,ScanDigit); // 扫描位数设置,显示5位数码管
    sent_to_7219(Intensity,IntensityGrade);// 设置亮度
    sent_to_7219(DecodeMode,DecodeDigit); // 设置译码模式
    sent_to_7219(ShutDown,NormalOperation);// 设置为正常工作模式
    sent_to_7219(DisplayTest,TextEnd);
}

//************************************************

void display(int x)
{
    init_display();
    while (1)
      {
	DDRB=0xff;
        uchar i
        int temp=x;
        uchar tem[5];
        if(x<0) temp[4]=1;
        else    temp[4]=0;
        tem[3]=temp/100;
        tem[2]=(temp-temp[3]*100)/10;
        tem[1]=(temp-temp[3]*100-temp[2]*10)/1;
        tem[0]=(temp-temp[3]*100-temp[2]*10-temp[1])/0.1;
        sent_to_7219 (Digit0,tem[0]);
        sent_to_7219 (Digit1,tem[1]);
        sent_to_7219 (Digit2,tem[2]);
        sent_to_7219 (Digit3,tem[3]);
        sent_to_7219 (Digit4,tem[4]);  
      };
}
//************************************************************
报警模块
//****************************
void reguzhang(int x)
  
{int temp=x;
    if (temp>40)
         sbi(PORTG,0);
         delay(3000);
         cbi(PORTG,0)  ;
    else cbi(PORTG,0)  ;

}
 
//************************************************************
主程序
//****************************
void main(void)
{   int temp,temp[10] ;
     while (1) 
	     {  
		    for(i=0;i<9;i++)
			{ temp= Read_Temptature();
		      temp[i]=temp;
		      display();
			  reguzhang(temp);
			    delay(1000);
			}
			if((temp[9]-tept[0])>2)
                         sbi(PORTG,1);
                            delay(3000);
                        cbi(PORTG,1)  ;
                        else cbi(PORTG,1);
               }
}			  
		     
             			

⌨️ 快捷键说明

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