新写的MSP430g2231数码管程序,附上原理图。
-
- #include "msp430.h"
- #include <stdint.h>
- #include "led.h"
-
- uint16_t t = 0,h = 0;
- uint32_t i = 0;;
- int main( void )
- {
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- LED_PORT_INIT();
- while(1)
- {
- if(i++%100==0)
- {
- setValue(t++,h++,0);
- }
- ledDisplay();
- // ledClearPartNumberValue();//消除残影
- __delay_cycles(500);
- }
- }
- #include "led.h"
- uint8_t fseg[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
- uint16_t tube1,tube2;
- uint8_t ledTwinkle = 0;//LED是否闪烁
- uint8_t ledTwinkleFlag = 0;//LED闪烁标志
- uint8_t showLed = 1;//LED是否显示
- uint8_t ledUnitCount = 1;//LED显示单元
- uint8_t displayUnitValue = 0;//LED单元数值
- void delay_us(uint16_t us)
- {
- }
- void setValue(uint16_t t,uint16_t h,uint8_t twinkle)
- {
- ledTwinkle = twinkle;
- tube1 = t;
- tube2 = h;
- }
- void putByte(uint8_t b)
- {
- uint8_t i;
- for(i=0;i<8;i++)
- {
- if(b&0x080)
- {
- DIO_H;
- }
- else
- {
- DIO_L;
- }
- b<<=1;
- delay_us(1);
- SCLK_H;
- delay_us(1);
- SCLK_L;
- delay_us(1);
- }
- }
- void show(void)
- {
- RCLK_H;
- delay_us(1);
- RCLK_L;
- delay_us(1);
- }
- void putNumber(uint8_t num,uint8_t bit)
- {
- putByte(~(1<<(7-bit)));
- putByte(fseg[num]|((bit == 2 || bit==6 )?0x80:0x00));
- show();
- }
- void refurbish(void)
- {
-
- }
-
- //清除残影
- void ledClearPartNumberValue(void)
- {
- putByte(0);
- putByte(0);
- show();
- }
- //设置小数点
- void ledSetPartDotValue(uint8_t dot)
- {
-
- }
- //设置显示单元
- void ledSetUnit(uint8_t unit)
- {
- switch(unit)
- {
- case 0:
- break;
- case 1:
- break;
- case 2:
- break;
- case 3:;
- break;
-
- }
- }
- //刷新数据
- void ledDisplay(void)
- {
- // ledClearPartNumberValue();//清除残影
- if(showLed && (ledTwinkle == 0 || ledTwinkleFlag))//判断显示标志
- {
- switch(ledUnitCount)
- {
- case 0:
- displayUnitValue = tube2%10;//个位
- break;
- case 1:
- displayUnitValue = (tube2%100)/10;//十位
- break;
- case 2:
- displayUnitValue = (tube2%1000)/100;//百位
- break;
- case 3:
- displayUnitValue = (tube2%10000)/1000;//千位
- break;
- case 4:
- displayUnitValue = tube1%10;//个位
- break;
- case 5:
- displayUnitValue = (tube1%100)/10;//十位
- break;
- case 6:
- displayUnitValue = (tube1%1000)/100;//百位
- break;
- case 7:
- displayUnitValue = (tube1%10000)/1000;//千位
- break;
- }
- putNumber(displayUnitValue,ledUnitCount);//显示单元数值
- if(ledUnitCount++>=7) ledUnitCount = 0;//下一单元
- }
- }
- //使能LED显示
- void ledEnable(void)
- {
- showLed = 1;
- }
- //禁能LED显示
- void ledDisable(void)
- {
- showLed = 0;
- }
- void SetLedTwinkle(uint8_t twinkle)
- {
- ledTwinkle = twinkle;
- }
- void test(uint8_t n)
- {
- putNumber(n,n);
- show();
- }
本帖最后由 littleshrimp 于 2014-7-23 16:28 编辑