專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

用430寫的IIC總線協(xié)議:24C02掉電記憶程序(數(shù)碼管顯示)

作者:我行天下   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月30日   【字體:

#include <msp430x14x.h>
#define OP_READ 0xa1        // 器件地址以及讀取操作  
#define OP_WRITE 0xa0       // 器件地址以及寫入操作  
#define uchar unsigned char
#define uint unsigned int
 #define SDA_OUT  P1DIR|=BIT3
#define SDA_IN  P1DIR&=~BIT3
#define SCL_OUT  P1DIR|=BIT2
#define SCL_IN  P1DIR&=~BIT2
 #define SDA_H  P1OUT|=BIT3
#define SDA_L  P1OUT&=~BIT3
#define SCL_H  P1OUT|=BIT2
#define SCL_L  P1OUT&=~BIT2
#define wei_h P5OUT|= BIT5
#define wei_l P5OUT&= ~BIT5
#define duan_l   P6OUT &= ~BIT6
#define duan_h  P6OUT |= BIT6
//數(shù)碼管7位段碼:0--f
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                    0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//記錄顯示位數(shù)的全局變量
uchar miao;
uchar write;
  //8M晶振時(shí)大約20us延時(shí)程序
void delayus(uint z)
{
    uint i,j;
    for(i = 0;i<20;i++)
      for(j=0;j<z;j++)
      {;;};
}
// 開始位  
void start() 

    SDA_OUT;
    SCL_OUT;
    SDA_H; 
    SCL_H; 
    delayus(1); //20
    SDA_L; 
    delayus(2);//40
    SCL_L; 

// 停止位
void stop()  

    SDA_OUT;
    SCL_OUT;
    SDA_L;  
  
    delayus(2);//20 
    SCL_H; 
    delayus(2);
    SDA_H; 
}   
// 從AT24Cxx移入數(shù)據(jù)到MCU  
uchar shin() 

    uchar i,read_data;
    SDA_OUT;
    SCL_OUT;
    for(i = 0; i < 8; i++) 
    { 
        SDA_H;
        delayus(1);
        SCL_H;
        SDA_IN;
        read_data <<= 1;    
        if(P1IN&BIT3)
          read_data|=0x01;
        SDA_OUT;      
        SCL_L; 
    }
    return(read_data); 

// 從MCU移出數(shù)據(jù)到AT24Cxx  
uchar shout(uint write_data) 

    uchar i; 
    uchar ack_bit;
    SDA_OUT;
    SCL_OUT;
    for(i = 0; i < 8; i++)       // 循環(huán)移入8個(gè)位  
    {      
          if(write_data&0x80)
          SDA_H;
          else
          SDA_L;
        delayus(2);//2
        SCL_H; 
        delayus(2);//2 
        SCL_L; 
        delayus(2);//2
        write_data <<= 1; 
    }
  
    SDA_H;  // 讀取應(yīng)答  
    delayus(2);
    SCL_H;  //注意拉高
    delayus(2);
    SDA_IN;
    if(P1IN&BIT3)
      ack_bit=1;
    else ack_bit=0;
    SCL_L; 
    return ack_bit; // 返回AT24Cxx應(yīng)答位  
}    
// 在指定地址addr處寫入數(shù)據(jù)write_data  
void write_byte(uchar addr, uchar write_data) 

    SDA_OUT;
    SCL_OUT;
    SDA_H; 
    SCL_H;
    start(); 
    shout(OP_WRITE); 
    shout(addr); 
    shout(write_data); 
    stop(); 
    delayus(1000);// 寫入周期  
}       
// 在當(dāng)前地址讀取  
uint read_current() 

    uint read_data; 
    start(); 
    shout(OP_READ); 
    read_data = shin(); 
    stop(); 
    return read_data; 

 
// 在指定地址讀取  
uchar read_random(uint random_addr) 

    SDA_IN;
    SCL_OUT;
    SDA_H; 
    SCL_H;
    start(); 
    shout(OP_WRITE); 
    shout(random_addr); 
    return(read_current()); 
}

void display(uchar dat)
{
  P4OUT = table[dat];//輸出段選信號(hào)
  duan_h;
  duan_l;
  P4OUT = 0x00; //設(shè)置P4,P5的輸出初值
  wei_h;
  wei_l;
}

void Clock(void)
{
miao++;
 if(miao==10)
   miao=0;
}

int main( void )
{
   /*下面六行程序關(guān)閉所有的IO口*/
    P1DIR = 0XFF;P1OUT = 0XFF;
    P2DIR = 0XFF;P2OUT = 0XFF;
    P3DIR = 0XFF;P3OUT = 0XFF;
    P4DIR = 0XFF;P4OUT = 0XFF;
    P5DIR = 0XFF;P5OUT = 0XFF;
    P6DIR = 0XFF;P6OUT = 0XFF;
  
  
    SDA_OUT;
    SCL_OUT;
    SDA_H; 
    SCL_H;
 
 
     WDTCTL = WDTPW + WDTHOLD;
     CCR0 = 32768 - 1;//設(shè)置定時(shí)器A的中斷時(shí)間為1S
    TACTL = TASSEL_1 + MC_1; //計(jì)數(shù)時(shí)鐘ACLK, 增計(jì)數(shù)模式
    CCTL0 |= CCIE; //使能CCR0比較中斷
    _EINT();//打開全局中斷
  
 
     miao=read_random(3) ;//從地址2中讀出以前保存的數(shù)據(jù)
     if(miao>9)    miao=0; //防止首次讀出錯(cuò)誤數(shù)據(jù)
  
      while(1)
      {
          display(miao);
        if(write==1)
      {
    write=0;
                  write_byte(3,miao);
   }
      
     LPM3;//進(jìn)入LPM3低功耗模式
          
      }
}

/*******************************************
函數(shù)名稱:TimerA_ISR
功    能:定時(shí)器A的中斷服務(wù)函數(shù)
********************************************/
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
    Clock();
    write=1;
   LPM3_EXIT;
}

關(guān)閉窗口

相關(guān)文章