TMS320F28335實現數碼管顯示

Mero技術部落格發表於2020-12-18

 

大家好,很高興又給大家寫部落格了。今天帶來的是關於數碼管顯示的文章。原理部分還請各位去看普中的視訊教程,很詳細了,我就不重複寫了,本文關注的是軟體程式,程式大部分都是抄的例程程式碼,但是優化了邏輯,不用判斷數字是不是整數。

一:硬體電路

圖一                                                                                                                                                                          圖二

 電路很簡單,就是一個數碼管驅動晶片,通過SPI協議傳輸資料,然後將8位二進位制資料依次送到數碼管的8個引腳端,圖中的數碼管其實每個有10個引腳,一個com公共端,8個數字顯示端,一個小數點。當com端為低電平,Q0~Q7輸出高電平的時候,數碼管就會亮。

 

接下來就是軟體部分,原程式是封裝了兩個函式,一個用來顯示整數,一個用來顯示小數,非常的不方便,我於是整合成一個函式,自動判斷是否為整數。

smg.c

/*
 * smg.c
 *
 *  Created on: 2009年1月1日
 *      Author: Administrator
 */


#include "smg.h"
unsigned  char smgduan[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
        0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
//00111111,00000110,01011011,01001111,01100110,01101101,01111101,00000111,
//01111111,01101111,01110111,01111100,00111001,01011110,01111001,01110001
unsigned  char smgduan_f[16] = {0xbF, 0x86, 0xdB, 0xcf, 0xe6, 0xed, 0xfD, 0x87,
        0xFF, 0xeF, 0xF7, 0xfC, 0xb9, 0xdE, 0xf9, 0xf1};
//10111111,10000110,11011011,11001111,11100110,11101101,11111101,10000111,
//11111111,11101111,11110111,11111100,10111001,11011110,11111001,11110001
void Init_SMG(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1; //open gpio clk

	GpioCtrlRegs.GPCMUX1.bit.GPIO70 = 0; //init GPIO70
	GpioCtrlRegs.GPCPUD.bit.GPIO70 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO70 = 1;

	GpioCtrlRegs.GPCMUX1.bit.GPIO71 = 0;//init GPIO71
	GpioCtrlRegs.GPCPUD.bit.GPIO71 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO71 = 1;

	GpioCtrlRegs.GPCMUX1.bit.GPIO72 = 0;//init GPIO72
	GpioCtrlRegs.GPCPUD.bit.GPIO72 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO72 = 1;

	GpioCtrlRegs.GPCMUX1.bit.GPIO73 = 0;//init GPIO73
	GpioCtrlRegs.GPCPUD.bit.GPIO73 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO73 = 1;

	GpioCtrlRegs.GPBMUX2.bit.GPIO54 = 0; //init GPIO54
	GpioCtrlRegs.GPBPUD.bit.GPIO54 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO54 = 1;

	GpioCtrlRegs.GPBMUX2.bit.GPIO56 = 0; //init GPIO56
	GpioCtrlRegs.GPBPUD.bit.GPIO56 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO56 = 1;
	EDIS;
	// default GPIO 70~73 low level
	GpioDataRegs.GPCCLEAR.bit.GPIO70 = 1;
	GpioDataRegs.GPCCLEAR.bit.GPIO71 = 1;
	GpioDataRegs.GPCCLEAR.bit.GPIO72 = 1;
	GpioDataRegs.GPCCLEAR.bit.GPIO73 = 1;
}
void HC164SendSPIData(unsigned char dat){
	char i;
	for(i=0;i<8;i++){
		SPICLKL; //spiclk init setting low level
		//such as 0D 10001111(0x8F), when get  MSB 1,  get the value by 0x8F&0x80 then >>7
		//we can get 00000001(0x01)
		if((dat&0x80)>>7){
			SPIMOSIH;//spidata setting to high level
		}else{
			SPIMOSIL;//spidata  setting to low level
		}
		DELAY_US(100);
		SPICLKH;
		dat<<=1;
	}
}
// used to show integer or float 

void SMGShowNum(float dat){
	Uint16 flag = 1;//default Integer not Float
	if((dat-(Uint16)dat)==0){
		flag = 1;
	}
	if((dat-(Uint16)dat)!=0){
		flag = 0;
	}
	if(flag){
			int i ;
			Uint16 buf[4];
			Uint16 *p = buf;
			*(p+3)=smgduan[(Uint16)dat%1000%100%10];//個位
			*(p+2)=smgduan[(Uint16)dat%1000%100/10];//十位
			*(p+1)=smgduan[(Uint16)dat%1000/100];//百位
			*(p)=smgduan[(Uint16)dat/1000];//千位
			for(i=0;i<4;i++){
				HC164SendSPIData(*(p++));
				switch(i){
					case 0: SEG1_SETH;SEG2_SETL;SEG3_SETL;SEG4_SETL;break;
					case 1: SEG1_SETL;SEG2_SETH;SEG3_SETL;SEG4_SETL;break;
					case 2: SEG1_SETL;SEG2_SETL;SEG3_SETH;SEG4_SETL;break;
					case 3: SEG1_SETL;SEG2_SETL;SEG3_SETL;SEG4_SETH;break;
				}
				DELAY_US(5000);
			}

	}
	/**
	 * such as a = 1.314 ,
	 * 1.314x10=13.14  [int(13.14)-int(1.314)]>10  ,point is laster first
	 * 13.14x10=131.4  [int(131.4)-int(13.14)]>100 ,point is laster second
	 * 131.4x10=1314   [int(1314)-int(131.4)]>1000 ,point is laster third
	 * 1314x10=13140   [int(13140)-int(1314)]>10000,point is laster forth
	 * */
	if(!flag){
		Uint16 buf[4];
		Uint16 *p = buf;
		Uint16 result;
		if(((Uint16)(dat*10)-(Uint16)dat)>10){
				result=dat*1000;
				*(p+3)=smgduan[(Uint16)result%1000%100%10];//個位
				*(p+2)=smgduan[(Uint16)result%1000%100/10];//十位
				*(p+1)=smgduan[(Uint16)result%1000/100];//百位
				*(p)=smgduan[(Uint16)result/1000]|0x80;//千位
		}
		if(((Uint16)(dat*10)-(Uint16)dat)>100){
			result=dat*100;
			*(p+3)=smgduan[(Uint16)result%1000%100%10];//個位
			*(p+2)=smgduan[(Uint16)result%1000%100/10];//十位
			*(p+1)=smgduan[(Uint16)result%1000/100]|0x80;//百位
			*(p)=smgduan[(Uint16)result/1000];//千位
		}
		if(((Uint16)(dat*10)-(Uint16)dat)>1000){
			result=dat*10;
			*(p+3)=smgduan[(Uint16)result%1000%100%10];//個位
			*(p+2)=smgduan[(Uint16)result%1000%100/10]|0x80;//十位
			*(p+1)=smgduan[(Uint16)result%1000/100];//百位
			*(p)=smgduan[(Uint16)result/1000];//千位
		}
		if(((Uint16)(dat*10)-(Uint16)dat)>10000){
			result=dat*1;
			*(p+3)=smgduan[(Uint16)result%1000%100%10]|0x80;//個位
			*(p+2)=smgduan[(Uint16)result%1000%100/10];//十位
			*(p+1)=smgduan[(Uint16)result%1000/100];//百位
			*(p)=smgduan[(Uint16)result/1000];//千位
		}
		int i ;
		for(i=0;i<4;i++){
			HC164SendSPIData(*(p++));
			switch(i){
				case 0: SEG1_SETH;SEG2_SETL;SEG3_SETL;SEG4_SETL;DELAY_US(5000);break;
				case 1: SEG1_SETL;SEG2_SETH;SEG3_SETL;SEG4_SETL;DELAY_US(5000);break;
				case 2: SEG1_SETL;SEG2_SETL;SEG3_SETH;SEG4_SETL;DELAY_US(5000);break;
				case 3: SEG1_SETL;SEG2_SETL;SEG3_SETL;SEG4_SETH;DELAY_US(5000);break;
			}
		}
	}

}

 

 

smg.h

/*
 * smg.h
 *
 *  Created on: 2009年1月1日
 *      Author: Administrator
 */

#ifndef APP_SMG_SMG_H_
#define APP_SMG_SMG_H_

#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

#define SPICLKH (GpioDataRegs.GPBSET.bit.GPIO56 = 1)
#define SPICLKL (GpioDataRegs.GPBCLEAR.bit.GPIO56 = 1)

#define SPIMOSIH (GpioDataRegs.GPBSET.bit.GPIO54 = 1)
#define SPIMOSIL (GpioDataRegs.GPBCLEAR.bit.GPIO54 = 1)

//數碼管位選訊號線管腳定義
#define SEG1_SETH			(GpioDataRegs.GPCSET.bit.GPIO70=1)
#define SEG1_SETL			(GpioDataRegs.GPCCLEAR.bit.GPIO70=1)
#define SEG2_SETH			(GpioDataRegs.GPCSET.bit.GPIO71=1)
#define SEG2_SETL			(GpioDataRegs.GPCCLEAR.bit.GPIO71=1)
#define SEG3_SETH			(GpioDataRegs.GPCSET.bit.GPIO72=1)
#define SEG3_SETL			(GpioDataRegs.GPCCLEAR.bit.GPIO72=1)
#define SEG4_SETH			(GpioDataRegs.GPCSET.bit.GPIO73=1)
#define SEG4_SETL			(GpioDataRegs.GPCCLEAR.bit.GPIO73=1)

void Init_SMG(void);
void HC164SendSPIData(unsigned char dat);
void SMGShowNum(float dat);

#endif /* APP_SMG_SMG_H_ */

 main.c

/*
 * main.c
 *
 *  Created on: 2020年5月31日
 *      Author: Administrator
 */


#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File
#include "key.h"
#include "leds.h"
#include "beep.h"
#include "relay.h"
#include "dc_motor.h"
#include "step_motor.h"
#include "timer0_int.h"
#include "smg.h"
void delay(void);

//



//#define 4LINE2
void main(){
	InitSysCtrl();
	InitPieCtrl();
	IER = 0x0000;
	IFR = 0x0000;
	InitPieVectTable();
    LED_Init();
    KEY_Init();
    BEEP_Init();
    StepMotorInit();
    Init_timer0_IRQn();
    Init_SMG();

//    the sequence of step_motor by working,1000,0100,0010,0001
    Uint32 STEP_MOTOR_ZTurn[] = {
			(GpioDataRegs.GPADAT.all|0x0000003c)&0xfffffff7,
	    	(GpioDataRegs.GPADAT.all|0x0000003c)&0xffffffdf,
    		(GpioDataRegs.GPADAT.all|0x0000003c)&0xfffffffb,
    		(GpioDataRegs.GPADAT.all|0x0000003c)&0xffffffef
    		};
    Uint32 STEP_MOTOR_FTurn[]   = {
    		(GpioDataRegs.GPADAT.all|0x0000003c)&0xffffffef,
    		(GpioDataRegs.GPADAT.all|0x0000003c)&0xfffffffb,
    	    (GpioDataRegs.GPADAT.all|0x0000003c)&0xffffffdf,
    		(GpioDataRegs.GPADAT.all|0x0000003c)&0xfffffff7
    		};
    Uint16 i = 0;
    int n=0;
    while(1){
    	    i++;
    	    if(i%5000==0){LED2_TOGGLE;}
    	    DELAY_US(100);
    	    BEEP_ON;
    	    n=100;
    	    SMGShowNum(1.234);

     }
}
void delay(void)
{
    Uint16 		i;
    Uint32      j;
	for(i=0;i<32;i++)
		for (j = 0; j < 100000; j++);
}

最後把程式奉上。

 

 

相關文章