×
单片机 > 单片机程序设计 > 详情

ADS7816的AVR驱动程序

发布时间:2020-08-21 发布时间:
|
/***********************************************************************************
相关寄存器设置参照M16单片机
初始化写:
SPI_Masterinit();
init_DA();
***********************************************************************************/
/****************************SPI片选硬件宏定义*************************************/
//各芯片的片选脚自行修改
#define Select_Volat      PORTD&=~(0x01<<6)     //置低PD6,选择电压采样ADS7816
#define un_Select_Volat   PORTD|=(0X01<<6)      //拉高PD6,放弃电压采样ADS7816
#define Select_Amp        PORTD&=~(0x01<<5)     //置低PD5,选择电流采样ADS7816
#define un_Select_Amp     PORTD|=(0X01<<5)      //拉高PD5,放弃电流采样ADS7816
#define Select_DA         PORTD&=~(0x01<<7)     //置低PD7,选择MAX5712
#define un_Select_DA      PORTD|=(0X01<<7)      //拉高PD7,放弃MAX5712

/******************************初始化SPI*******************************************/
void SPI_Masterinit(void)
{
    
    PORTB|=(0x01<<6);       //set bit MISO
    PORTB&=~(0x01<<5);     
    PORTB&=~(0x01<<7);     // clear bits MOSI, & SCK
    DDRB|=0xb0;            // Set SCK, MOSI & SS as outputs
    //SPCR=BIT(MSTR)|BIT(CPHA)|BIT(SPR1);
    SPCR=BIT(SPE)|BIT(MSTR)|BIT(CPOL);
    SPSR=0x01;
    
}
/***************************SPI数据传递函数****************************************/

void SPI_WriteByte(unsigned char data)

  SPDR = data;                              // 启动数据传输 
  while(!(SPSR & (1< }

unsigned char SPI_ReadByte(void)
{
  SPDR = 0xff;
  while(!(SPSR & (1<   return SPDR;                              //返回数据 
}
unsigned int volat(void)   
{   
    unsigned int c=0;   
    unsigned int out= 0;   
    un_Select_Volat; 
    Select_Volat;                           //开始采样
    SPDR = 0x00;   
    while(!(SPSR & (1<     c = SPDR&(0b01111111)&(0b10111111);//前三位无效,从第四位开始B11,B10...    
    out = c;//第一次取得5个有效位    
    out = out<<7;   
   
    SPDR = 0x00;   
    while(!(SPSR & (1<     c = SPDR;//第二次取得7个有效位    
   
    out += (c>>1);   
    //结束采样    
    un_Select_Volat;   
    return out;   

}  


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
如何升级STM32单片机的代码