⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3310main.c

📁 msp430f149的3110液晶和内部ADC的调试程序
💻 C
字号:
#include <msp430x14x.h>
#include "english_6x8_pixel.h"  //英文字库
#include "write_chinese_string_pixel.h"	//汉字库
#define   uint  unsigned int
#define   uchar unsigned char

//*******************************************************************
	//定义3310引脚
//***************************************************************
#define LCD_DC                 0x410       //  LCD第4脚, Msp430:P444 
#define LCD_CE                 0x40      //  LCD第5脚,  Msp430:P46 
#define SPI_MOSI               0x04        //  LCD第3脚,  Msp430:P42
#define LCD_RST                0x02        //  LCD第8脚, Msp430:P41
#define SPI_CLK                0x01        //  LCD第2脚, Msp430:P40(

/*-------------------------------------
		定义延时函数
---------------------------------------------*/
#define CPU_F ((double)8000000) 
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) 
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) 


unsigned int results1=0;
unsigned int results2=0;
unsigned int results3=0;
unsigned int AD_TEMP=0;
unsigned char adc_flag=0;
//float AD=0;
/*---------------------------------------------

----------------------------------*/
void LCD_init(void);

void LCD_clear(void);

void LCD_write_english_string(unsigned char X,unsigned char Y,char *s);

void LCD_write_chinese_string(unsigned char X, unsigned char Y,
                   unsigned char ch_with,unsigned char num,
                   unsigned char line,unsigned char row);
void LCD_move_chinese_string (unsigned char X, unsigned char Y, unsigned char T);
void LCD_move_chinese_string(unsigned char X, unsigned char Y, unsigned char T);                 

void LCD_write_char(unsigned char c);

void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map,
                  unsigned char Pix_x,unsigned char Pix_y);
                  
void LCD_write_byte(unsigned char data, unsigned char dc);

void LCD_write_number(/*unsigned char X,unsigned char Y,*/unsigned char a);
//-------------------------------------------------------------

/*-----------------------------------------------------------------------
LCD_init          : 3310LCD初始化
-----------------------------------------------------------------------*/
void LCD_init(void)
  {
    P4OUT &= ~LCD_RST;          // 产生一个让LCD复位的低电平脉冲
    delay_us(1);
    P4OUT |= LCD_RST;
    P4OUT &= ~LCD_CE ;		// 关闭LCD
    delay_us(1);
    P4OUT |= LCD_CE;		// 使能LCD
    delay_us(1);
    LCD_write_byte(0x21, 0);	// 使用扩展命令设置LCD模式
    LCD_write_byte(0xc8, 0);	// 设置偏置电压
    LCD_write_byte(0x06, 0);	// 温度校正
    LCD_write_byte(0x13, 0);	// 1:48
    LCD_write_byte(0x20, 0);	// 使用基本命令
    LCD_clear();	        // 清屏
    LCD_write_byte(0x0c, 0);	// 设定显示模式,正常显示
        
    P4OUT &= ~LCD_CE ;          // 关闭LCD
  }

/*-----------------------------------------------------------------------
LCD_clear         : LCD清屏函数
-----------------------------------------------------------------------*/
void LCD_clear(void)
  {
    unsigned int i;

    LCD_write_byte(0x0c, 0);			
    LCD_write_byte(0x80, 0);			

    for (i=0; i<504; i++)
      LCD_write_byte(0, 1);			
  }

/*-----------------------------------------------------------------------
LCD_set_XY        : 设置LCD坐标函数
输入参数:X       :0-83
          Y       :0-5
-----------------------------------------------------------------------*/
void LCD_set_XY(unsigned char X, unsigned char Y)
  {
    LCD_write_byte(0x40 | Y, 0);		// column
    LCD_write_byte(0x80 | X, 0);          	// row
  }

/*-----------------------------------------------------------------------
LCD_write_char    : 显示英文字符
输入参数:c       :显示的字符;
-----------------------------------------------------------------------*/
void LCD_write_char(unsigned char c)
  {
    unsigned char line;

    c -= 32;

    for (line=0; line<6; line++)
      LCD_write_byte(font6x8[c][line], 1);
  }

/*-----------------------------------------------------------------------
LCD_write_english_String  : 英文字符串显示函数
输入参数:*s      :英文字符串指针;
          X、Y    : 显示字符串的位置,x 0-83 ,y 0-5

-----------------------------------------------------------------------*/
void LCD_write_english_string(unsigned char X,unsigned char Y,char *s)
  {
    LCD_set_XY(X,Y);
    while (*s) 
      {
	 LCD_write_char(*s);
	 s++;
      }
  }
/*-----------------------------------------------------------------------
LCD_write_chinese_string: 在LCD上显示汉字
输入参数:X、Y    :显示汉字的起始X、Y坐标;
          ch_with :汉字点阵的宽度
          num     :显示汉字的个数;  
          line    :汉字点阵数组中的起始行数
          row     :汉字显示的行间距
编写日期          :2004-8-11 
最后修改日期      :2004-8-12 
测试:
	LCD_write_chi(0,0,12,7,0,0);
	LCD_write_chi(0,2,12,7,0,0);
	LCD_write_chi(0,4,12,7,0,0);	
-----------------------------------------------------------------------*/                        
void LCD_write_chinese_string(unsigned char X, unsigned char Y, 
                   unsigned char ch_with,unsigned char num,
                   unsigned char line,unsigned char row)
  {
    unsigned char i,n;
    
    LCD_set_XY(X,Y);                             //设置初始位置
    
    for (i=0;i<num;)
      {
      	for (n=0; n<ch_with*2; n++)              //写一个汉字
      	  { 
      	    if (n==ch_with)                      //写汉字的下半部分
      	      {
      	        if (i==0) LCD_set_XY(X,Y+1);
      	        else
      	           LCD_set_XY((X+(ch_with+row)*i),Y+1);
              }
      	    LCD_write_byte(write_chinese_string[line+i][n],1);
      	  }
      	i++;
      	LCD_set_XY((X+(ch_with+row)*i),Y);
      }
  }
  

/*-----------------------------------------------------------------------
LCD_draw_map      : 位图绘制函数

输入参数:X、Y    :位图绘制的起始X、Y坐标;
          *map    :位图点阵数据;
          Pix_x   :位图像素(长)
          Pix_y   :位图像素(宽)
-----------------------------------------------------------------------*/
void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,unsigned char *map,
                  unsigned char Pix_x,unsigned char Pix_y)
  {
    unsigned int i,n;
    unsigned char row;
    
    if (Pix_y%8==0) row=Pix_y/8;      //计算位图所占行数
      else
        row=Pix_y/8+1;
    
    for (n=0;n<row;n++)
      {
      	LCD_set_XY(X,Y);
        for(i=0; i<Pix_x; i++)
          {
            LCD_write_byte(map[i+n*Pix_x], 1);
          }
        Y++;                         //换行
      }      
  }

/*-----------------------------------------------------------------------
LCD_write_byte    : 使用模拟SPI接口写数据到LCD
输入参数:data    :写入的数据;
          command :写数据/命令选择;
-----------------------------------------------------------------------*/
void LCD_write_byte(unsigned char data, unsigned char command)
  {
    unsigned char i;
    P4OUT &= ~LCD_CE ;		        // 使能LCD
    
    P4OUT&=~SPI_CLK;
    
    if (command == 0)
      P4OUT &= ~LCD_DC;	        // 传送命令
    else
      P4OUT |= LCD_DC;	        // 传送数据
    
    for(i=8;i>0;i--)
	{
	 P4OUT&=~SPI_CLK;
         delay_us(1);
	 if(data&BIT7)
               P4OUT|= SPI_MOSI;	//送1
	 else  P4OUT&=~SPI_MOSI;	//送0
        
         delay_us(1);        
	 P4OUT|=SPI_CLK;      		//上升沿上传数据
          data<<=1;
	}
      delay_us(1);
     P4OUT&=~SPI_CLK;
       delay_us(1);
     P4OUT|=SPI_CLK; 
     
    P4OUT|= LCD_CE ;			// 关闭LCD
  }

//------------------------------
 		//初始化系统时钟
//----------------------------------------*/
void int_clk()   
{
    unsigned char i;
    BCSCTL1&=~XT2OFF;  //打开XT振荡器
    BCSCTL2|=SELM1+SELS;//MCLK 8M and SMCLK 1M  
    do
	{
        IFG1 &= ~OFIFG;                 //清除振荡错误标志 
        for(i = 0; i < 100; i++)
	  _NOP();			//延时等待
        }
    while ((IFG1 & OFIFG) != 0);	//如果标志为1继续循环等待
    IFG1&=~OFIFG;
}
/********************************/
//函数功能:显示数字//编辑: 刘家松//2009年5月10日
/*************************************/
void LCD_write_number(/*unsigned char X,unsigned char Y,*/unsigned char a)
{
    LCD_write_char(a+16+32);
}
/***************/
//初始化ADC//刘家松//09-05-10
/******************/
void int_adc()
{
  P6SEL |= 0X0f;                              //选择AD通道
  ADC12CTL0=ADC12ON+MSC+SHT0_7+REFON+REF2_5V; //采样保持时间为16个ADC12CLK,参考电压开启,选择2.5V
  ADC12CTL1=SHP+CONSEQ_3;                      //多通道重复转换模式---单多通道时主哟设置此寄存器
  ADC12MCTL0 = 0X20+0x00;                        // ref+=REF2_5V, channel = A0
  ADC12MCTL1 = 0X20+0x01;                      // ref+=REF2_5V, channel = A1
  ADC12MCTL2 = 0X20+0x02;                       //ref+=REF2_5V, channel = A2
  ADC12MCTL3 = 0X20+0x03;                  // ref+=AVcc, channel = A3, 结束转换通道选择.
  
  ADC12IE = 0x0f;                            // 使能相应通道转换中断
  ADC12CTL0 |= ENC;                          // 使能AD转换器
}

#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
  adc_flag=1;
  AD_TEMP = ADC12MEM0;                  // 读转A0换值
   results1 = ADC12MEM1;               // 读转A1换值
   results2 = ADC12MEM2;               // 读转A2换值
   results3 = ADC12MEM3;               // 读转A3换值
  ADC12IE&= ~0x0f; 
}

/*******************************/
//显示电压函数//刘家松09-05-10
/*-----------------------------*/
void print_float(float data)
{
    uint temp;
    uchar a1,a2,a3,a4;
    data=data*2.5/4095;
    data=data*1000;

    temp=(uint)data;
    
    a1=temp/1000;
    a2=temp%1000/100;
    a3=temp%1000%100/10;
    a4=temp%1000%100%10;
    
    LCD_write_number(a1);
    LCD_write_char('.');
    LCD_write_number(a2);
    LCD_write_number(a3);
    LCD_write_number(a4); 
    LCD_write_char('V');
}
/*-----------------------*/



int main()
{
 //float AD;
 //int count;
 WDTCTL=WDTPW+WDTHOLD;     //关闭看门狗
 int_clk();			//初始化系统时钟
 int_adc();//初始化ADC 
 P4DIR=0xff;                 //p1 output
 P4OUT=0xff;
 LCD_init();			//初始化LCD
 _EINT();  //使能中断 
 ADC12CTL0|=ADC12SC;  //开启转换
 while(1)  
      { 
       if(adc_flag==0)
        {
          ADC12CTL0|=ADC12SC;  //开启转换
          ADC12IE=0x0f;
         }
 if(adc_flag==1)
    {    
      //  AD+=AD_TEMP;
       // count++;
       // if(count==1000)
       // {
       // AD=AD/1000;  

        LCD_set_XY(0,0); 
        print_float(AD_TEMP);
        
        LCD_set_XY(0,1); 
        print_float(results1);

         LCD_set_XY(0,2); 
         print_float(results2);
        
         LCD_set_XY(0,3); 
         print_float(results3);
        
        //AD=0;
        //count=0;
        AD_TEMP=0;//变量清零
     //  }
        adc_flag=0;//清中断标志       
   } 
       }	  
}











⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -