当前位置:文档之家› 数控直流电流源程序

数控直流电流源程序

数控直流电流源程序/*跳线说明:1)将EXP-LM3S811板卡上JP9、JP13跳至左侧(短接1-2);2)将EXP-min_system_board板卡上JP13、JP14、JP15、JP16跳至右侧(短接2-3。

操作过程:1)将EXP-min_system_board板卡上K1拨动开关拨至ON状态,给液晶上电;2)调节RP1电位器,使液晶有合适的背光;3)上电,编译并下载程序,复位后全速运行程序;观察液晶显示的内容,再修改程序使之显示自己的内容。

*/#include "systemInit.h"#include "ADS7886.h"#include "TLV5616.h"#include "timer.h"#define CTL_PERIPH SYSCTL_PERIPH_GPIOC// 控制液晶所用的片内端口外设定义#define CTL_PORT GPIO_PORTC_BASE#define SCK GPIO_PIN_4 // 定义信号SCK#define SID GPIO_PIN_5 // 定义信号SID#define CS GPIO_PIN_6 // 定义信号CS#define PSB GPIO_PIN_7 // 定义信号PSB#define SCK_L GPIOPinWrite(CTL_PORT,SCK,0x00) // 定义信号输出低电平#define SID_L GPIOPinWrite(CTL_PORT,SID,0x00)#define CS_L GPIOPinWrite(CTL_PORT,CS,0x00)#define PSB_L GPIOPinWrite(CTL_PORT,PSB,0x00)#define SCK_H GPIOPinWrite(CTL_PORT,SCK,0xFF) // 定义信号输出高电平#define SID_H GPIOPinWrite(CTL_PORT,SID,0xFF)#define CS_H GPIOPinWrite(CTL_PORT,CS,0xFF)#define PSB_H GPIOPinWrite(CTL_PORT,PSB,0xFF)#define SID_READ GPIOPinRead(CTL_PORT,SID)// 定义读回的数据#define SID_IN GPIOPinTypeGPIOInput(CTL_PORT,SID) // 定义SID信号为输入#define SID_OUT GPIOPinTypeGPIOOutput(CTL_PORT,SID) //定义SID信号为输出#define LED_PERIPH SYSCTL_PERIPH_GPIOB#define LED_PORT GPIO_PORTB_BASE#define LED GPIO_PIN_5#define KEY_PERIPH SYSCTL_PERIPH_GPIOD// KEYS所接的端口#define KEY_PORT GPIO_PORTD_BASE#define KEYGPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4|GPIO_PIN_3|GPIO_ PIN_2|GPIO_PIN_1|GPIO_PIN_0#define KEY_HGPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4#define KEY_LGPIO_PIN_3|GPIO_PIN_2|GPIO_PIN_1|GPIO_PIN_0#define key_h_in GPIOPinTypeGPIOInput(KEY_PORT,KEY_H) #define key_l_in GPIOPinTypeGPIOInput(KEY_PORT,KEY_L) #define key_h_outGPIOPinTypeGPIOOutput(KEY_PORT,KEY_H)#define key_l_outGPIOPinTypeGPIOOutput(KEY_PORT,KEY_L)#define SysCtlPeriClkGating SysCtlPeripheralClockGating //定义睡眠函数#define SysCtlPeriSlpEnable SysCtlPeripheralSleepEnable#define BitRate 115200 // 设定SPI的波特率#define DataWidth 8 // 设定SPI的数据宽度unsigned char AC_TABLE[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行汉字位置0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行汉字位置} ;//开始菜单欢迎界面unsigned char Hello1[]="欢迎使用——数控" ;unsigned char Hello2[]="直流电流源" ;unsigned char Hello3[]="制作人: " ;unsigned char Hello4[]="chenfangjie";//一级菜单1unsigned char menu1_1[]="请输入(20~2000):"; // 选择提示unsigned char menu1_2[]="设值: mA ";//一级菜单1unsigned char menu2_1[]="设定值: mA"; // 选择提示unsigned char menu2_2[]="读取值: A";unsigned char key_val=0; //键值全局变量int A;void keyBoardInit(void){SysCtlPeripheralEnable(KEY_PERIPH); //D口使能开GPIOPinTypeGPIOOutput(KEY_PORT,KEY_H); //扫描信号输出GPIOPinTypeGPIOInput(KEY_PORT,KEY_L); //按键信号输入}//键盘扫描程序void keyBoard_scan(void){//如下依次输出行扫描线//如果有键按下则扫描列线,并获得键值//按键释放判断unsigned char key_h,key_l;key_val=0;key_l_out;key_h_out;GPIOPinWrite(KEY_PORT,KEY,0xf0);key_h_in;key_h=GPIOPinRead(KEY_PORT,KEY_H);if((GPIOPinRead(KEY_PORT,KEY_H)&0xf0)!=0xf0){SysCtlDelay(5*(TheSysClock/3000));if((GPIOPinRead(KEY_PORT,KEY_H)&0xf0)!=0xf0){key_h=GPIOPinRead(KEY_PORT,KEY_H)&0xf0;key_h_out;key_l_out;GPIOPinWrite(KEY_PORT,KEY,0x0f);key_l_in;key_l=GPIOPinRead(KEY_PORT,KEY_L)&0x0f;key_val=key_h+key_l;while(key_l!=0x0f){key_l=GPIOPinRead(KEY_PORT,KEY_L)&0x0f;}switch(key_val){case 0xe7: key_val=13;break;case 0xeb: key_val=9;break;case 0xed: key_val=5;break;case 0xee: key_val=1;break;case 0xd7: key_val=14;break;case 0xdb: key_val=10;break;case 0xdd: key_val=6;break;case 0xde: key_val=2;break;case 0xb7: key_val=15;break;case 0xbb: key_val=11;break;case 0xbd: key_val=7;break;case 0xbe: key_val=3;break;case 0x77: key_val=16;break;case 0x7b: key_val=12;break;case 0x7d: key_val=8;break;case 0x7e: key_val=4;break;}}}}// LCM端口初始化void init(void){SysCtlPeripheralEnable( CTL_PERIPH );// 使能所用的端口GPIODirModeSet(CTL_PORT, (SCK | PSB | CS ) ,GPIO_DIR_MODE_OUT);// 设置信号的方向GPIOPadConfigSet(CTL_PORT, (SCK | PSB | CS ),GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU); //设置IO的驱动能力SysCtlPeripheralEnable(LED_PERIPH);GPIOPinTypeGPIOOutput(LED_PORT, LED);GPIOPadConfigSet(LED_PORT, LED, GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU);}// 串行方式发送一个字节void SendByte(unsigned char Dbyte){unsigned char i ;for(i=0 ;i< 8 ;i++){SCK_L ;SID_OUT;if (Dbyte & 0x80){SID_H;}else{SID_L;}Dbyte=Dbyte<<1;SCK_H;SCK_L;}}// 串行方式接收一个字节unsigned char ReceiveByte(void){unsigned char i,temp1,t,temp2;temp1=temp2=0;for(i=0;i<8;i++){temp1=temp1<<1;SCK_L;SCK_H;SCK_L;SID_IN;t=SID_READ;if (t){temp1++;}}for(i=0 ;i<8 ;i++){temp2=temp2<<1;SCK_L;SCK_H;SCK_L;SID_IN;t=SID_READ;if (t){temp2++;}}return ((0xf0&temp1)+(0x0f&temp2)); }// 判断是否忙碌void CheckBusy( void ){do SendByte(0xfc); //字节格式:11111,RW(1),RS(0),0while(0x80&ReceiveByte());// 判断是否忙碌:BF(.7)=1 Busy}// 向液晶发送命令void WriteCommand( unsigned char Cbyte ){CS_H;CheckBusy();SendByte(0xf8); //字节格式:11111,RW(0),RS(0),0SendByte(0xf0&Cbyte);// 高四位SendByte(0xf0&Cbyte<<4);// 低四位(先执行< ;< ;)CS_L;}// 向液晶发送显示数据void WriteData( unsigned char Dbyte )//显示字符{CS_H;CheckBusy();SendByte(0xfa); //字节格式:11111,RW(0),RS(1),0SendByte(0xf0&Dbyte);// 高四位SendByte(0xf0&Dbyte<<4);// 低四位(先执行< ;< ;)CS_L;}// 延时函数void Delay(unsigned int MS){unsigned char us,usn;while(MS!=0){usn = 2;while(usn!=0){us=0xf5 ;while (us!=0){us-- ;};usn--;}MS--;}}// 维捷登测试架专用延时函数void DelayKey(unsigned int Second , unsigned int MS100){ //输入精确到0.1S,是用","unsigned int i;for(i=0 ;i<Second*100+MS100*10 ;i++){Delay(10) ;}}// 文本区清RAM函数void LcmClearTXT( void ){unsigned char i ;WriteCommand(0x30);// 8BitMCU,基本指令集合WriteCommand(0x80);// AC归起始位for(i=0 ;i<64 ;i++)WriteData(0x20);}// 液晶初始化函数void LcmInit( void ){WriteCommand(0x30);// 8BitMCU,基本指令集合WriteCommand(0x03);// AC归0,不改变DDRAM内容WriteCommand(0x0c);// 显示ON,游标Off,游标位反白OFFWriteCommand(0x01);// 清屏,AC归0WriteCommand(0x06);// 写入时,游标右移动}void PutStr(unsigned char row,unsigned char col,unsigned char *puts) {int i=0;WriteCommand(0x30);// 8BitMCU,基本指令集合WriteCommand(AC_TABLE[8*row+col]);// 起始位置for(i=0;puts[i]!='\0';i++){WriteData(puts[i]);}}void DisplayHello(void) //显示欢迎菜单{LcmClearTXT();PutStr(0,0,Hello1);PutStr(1,0,Hello2);DelayKey(2,0);PutStr(0,0,Hello3);PutStr(1,0,Hello4);DelayKey(2,0);}void DisplayMenu1_1(void) //一级菜单显示{unsigned char a[4];int i=0,j;float v;key_val=0;LcmClearTXT();PutStr(0,0,menu1_1);PutStr(1,0,menu1_2);WriteCommand(0x30);WriteCommand(0x93);while(!(key_val==4)) // 如果按键D没有按下一直显示一级菜单{switch(key_val){case 1:WriteData('0');a[i]=0;i++;while(key_val==1);break;case 2:;break;case 3:;break;case 4:;break;case 5:WriteData('1');a[i]=1;i++;while(key_val==5);break;case 6:WriteData('2');a[i]=2;i++;while(key_val==6);break;case 7:WriteData('3');a[i]=3;i++;while(key_val==7);break;case 8:;break;case 9:WriteData('4');a[i]=4;i++;while(key_val==9);break;case 10:WriteData('5');a[i]=5;i++;while(key_val==10);break;case 11:WriteData('6');a[i]=6;i++;while(key_val==11);break;case 12:;break;case 13:WriteData('7');a[i]=7;i++;while(key_val==13);break;case 14:WriteData('8');a[i]=8;i++;while(key_val==14);break;case 15:WriteData('9');a[i]=9;i++;while(key_val==15);break;case 16:;break;}}i--;if (i==1){A=a[0]*10+a[1];}elseif(i==2){A=a[0]*100+a[1]*10+a[2];}elseif(i==3){A=a[0]*1000+a[1]*100+a[2]*10+a[3];if(A>2000)DisplayMenu1_1();}for(j=0;j<=i;j++){a[j]=a[j]+0x30;}LcmClearTXT();PutStr(0,0,menu2_1);PutStr(1,0,menu2_2);PutStr(0,4,a);while(1){while(!(key_val==4)) // 如果按键D没有按下一直显示一级菜单{switch(key_val){case 1:;break;case 2:A++;while(key_val==2);break; //数值加1case 3:A--;while(key_val==3);break; //数值减1case 4:;break;case 5:;break;case 6:;break;case 7:;break;case 8:;break;case 9:;break;case 10:;break;case 11:;break;case 12:;break;case 13:;break;case 14:;break;case 15:;break;case 16:;break;}if(A>=20&&A<=99){a[0]=A/10+0x30;a[1]=A%10+0x30;a[2]=0x20;a[3]=0x20;}if(A>=100&&A<=999){a[0]=A/100+0x30;a[1]=A/10%10+0x30;a[2]=A%10+0x30;a[3]=0x20;}if(A>=1000&&A<=2000){a[0]=A/1000+0x30;a[1]=A/100%10+0x30;a[2]=A/10%10+0x30;a[3]=A%10+0x30;}PutStr(0,4,a);v=(int)(((float)A/1000/2.5)*4096);DA_Write(v);AD_Display();}DisplayMenu1_1();}}// 主函数(程序入口)int main(void){jtagWait();// JTAG口解锁函数clockInit(); // 时钟初始化init();// 端口初始化PSB_L; // 串行方式Delay(100); // 延时LcmInit();// 液晶初始化keyBoardInit(); // 键盘初始化DA_Init();AD_Init();//SysCtlPeriClkGating(true); // 允许在睡眠模式下外设采用寄存器SCGCn配置时钟//SysCtlPeriSlpEnable(KEY_PERIPH); // 允许KEY 所在GPIO端口在睡眠模式下继续工作SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 ); // 使能定时器0外设TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC); // 设置定时器0为周期触发模式TimerLoadSet(TIMER0_BASE, TIMER_A, 300000); // 设置定时器装载值:定时10MSTimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // 设置定时器为溢出中断TimerEnable(TIMER0_BASE, TIMER_A); // 使能定时器0IntEnable(INT_TIMER0A); // 使能定时器0外设//DisplayHello();IntMasterEnable(); // 处理器总中断使能//GPIOPinWrite(LED_PORT, LED,0x00);DisplayMenu1_1(); //一级菜单显示}//定时器中断void Timer0A_ISR(void)// 定时器0中断处理程序{TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // 清除定时器0中断keyBoard_scan();TimerEnable(TIMER0_BASE, TIMER_A); // 使能定时器0 }。

相关主题