MSP430搭的一个简易电压表,上位机用JAVA写的

来源:互联网 发布:分析大数据的意义 编辑:程序博客网 时间:2024/05/15 23:47
Code:
  1. import java.io.*;   
  2. import java.util.*;   
  3. import javax.comm.*;   
  4. import javax.swing.*;   
  5.   
  6. public class SimpleRead implements Runnable, SerialPortEventListener {   
  7.     static CommPortIdentifier portId;   
  8.     static Enumeration portList;   
  9.   
  10.     InputStream inputStream;   
  11.     SerialPort serialPort;   
  12.     Thread readThread;   
  13.   
  14.     static  JTextField textfield;   
  15.     public static void UiPaint()   
  16.     {   
  17.         JFrame frame = new JFrame("电压转换软件-徐方鑫");   
  18.         frame.setSize(250,70);   
  19.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  20.         JPanel panel = new JPanel();   
  21.         frame.add(panel);   
  22.         JLabel label = new JLabel("电压检测为:");   
  23.         panel.add(label);   
  24.         textfield = new JTextField(6);   
  25.         textfield.setHorizontalAlignment(JTextField.CENTER);     
  26.         panel.add(textfield);   
  27.         //textfield.enable(false);  
  28.         JLabel label2 = new JLabel("V");   
  29.         panel.add(label2);   
  30.         frame.setVisible(true);   
  31.     }   
  32.   
  33.     public static void main(String[] args) {   
  34.     UiPaint();   
  35.         portList = CommPortIdentifier.getPortIdentifiers();   
  36.   
  37.         while (portList.hasMoreElements()) {   
  38.             portId = (CommPortIdentifier) portList.nextElement();   
  39.             if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {   
  40.                 if (portId.getName().equals("COM1")) {   
  41.                 //if (portId.getName().equals("/dev/term/a")) {  
  42.                     SimpleRead reader = new SimpleRead();   
  43.                 }   
  44.             }   
  45.         }   
  46.     }   
  47.   
  48.     public SimpleRead() {   
  49.         try {   
  50.             serialPort = (SerialPort) portId.open("SimpleReadApp"2000);   
  51.         } catch (PortInUseException e) {}   
  52.         try {   
  53.             inputStream = serialPort.getInputStream();   
  54.         } catch (IOException e) {}   
  55.     try {   
  56.             serialPort.addEventListener(this);   
  57.     } catch (TooManyListenersException e) {}   
  58.         serialPort.notifyOnDataAvailable(true);   
  59.         try {   
  60.             serialPort.setSerialPortParams(9600,   
  61.                 SerialPort.DATABITS_8,   
  62.                 SerialPort.STOPBITS_1,   
  63.                 SerialPort.PARITY_NONE);   
  64.         } catch (UnsupportedCommOperationException e) {}   
  65.         readThread = new Thread(this);   
  66.         readThread.start();   
  67.     }   
  68.   
  69.     public void run() {   
  70.         try {   
  71.             Thread.sleep(20000);   
  72.         } catch (InterruptedException e) {}   
  73.     }   
  74.   
  75.     public void serialEvent(SerialPortEvent event) {   
  76.         switch(event.getEventType()) {   
  77.         case SerialPortEvent.BI:   
  78.         case SerialPortEvent.OE:   
  79.         case SerialPortEvent.FE:   
  80.         case SerialPortEvent.PE:   
  81.         case SerialPortEvent.CD:   
  82.         case SerialPortEvent.CTS:   
  83.         case SerialPortEvent.DSR:   
  84.         case SerialPortEvent.RI:   
  85.         case SerialPortEvent.OUTPUT_BUFFER_EMPTY:   
  86.             break;   
  87.         case SerialPortEvent.DATA_AVAILABLE:   
  88.             byte[] readBuffer = new byte[6];   
  89.   
  90.             try {   
  91.                 while (inputStream.available() > 0) {   
  92.                     int numBytes = inputStream.read(readBuffer);   
  93.                 }   
  94.                 //System.out.print(new String(readBuffer));  
  95.         textfield.setText(new String(readBuffer));   
  96.             } catch (IOException e) {}   
  97.             break;   
  98.         }   
  99.     }   
  100. }   
Code:
  1. #include  <msp430x14x.h>   
  2. #include  "Init.h"   
  3. /******************************************* 
  4. 函数名称:Init_AD  
  5. 功    能:AD初始化函数  
  6. 参    数:无         
  7. 返回值  :无  
  8. ********************************************/  
  9. void Init_AD(void)   
  10. {   
  11.   P6SEL |= 0x01;                            // 使能ADC通道  
  12.   ADC12CTL0 = ADC12ON+SHT0_8+MSC;           // 打开ADC,设置采样时间  
  13.   ADC12CTL1 = SHP+CONSEQ_2;                 // 使用采样定时器  
  14.   ADC12IE = 0x01;                           // 使能ADC中断  
  15.   ADC12CTL0 |= ENC;                         // 使能转换  
  16.   ADC12CTL0 |= ADC12SC;                     // 开始转换  
  17. }   
  18. /*******************************************  
  19. 函数名称:Init_watchdog  
  20. 功    能:看门狗初始化函数  
  21. 参    数:无         
  22. 返回值  :无  
  23. ********************************************/  
  24. void Init_watchdog(void// WatchdogInit  
  25. {   
  26.     WDTCTL=WDTPW+WDTHOLD;// Stop watchdog   
  27. }   
  28. /*******************************************  
  29. 函数名称:Init_uart  
  30. 功    能:串口初始化  
  31. 参    数:无         
  32. 返回值  :无  
  33. ********************************************/  
  34. void Init_uart(int Bund) // Uart_Init  
  35. {   
  36.     P3SEL|=0xc0;         // P3.6,7 = USART1 TXD/RXD  
  37.     P3DIR |= 0x40;       // 串口发送数据端口为输出,接收数据端口为输入  
  38.     UCTL1=SWRST;         // Initialize USART state machine  
  39.     UTCTL1|=SSEL0;       // UCLK = ACLK  
  40.     UBR01=32000/Bund;    // 9600 from 1Mhz,波特率设置32k/9600 - 13.65  
  41.     UBR11=0x00;   
  42.     UMCTL1=0x4a;         // Modulation   
  43.     UCTL1=CHAR;          // 8 bits data format. enable UART  
  44.     IFG2&=~UTXIFG1;      // Enable USART0 RX/TX interrupt  
  45.     IE2|=URXIE1;         // Enable receive interrupt.  
  46.     ME2|=URXE1+UTXE1;    // Enabled USART1 TXD/RXD  
  47.     //_BIS_SR(LPM3_bits + GIE);    // Enter LPM3 w/ interrupt  
  48. }  
Code:
  1. #define uchar unsigned char   
  2. #define uint unsigned int   
  3. void Init_uart(int Bund); // Uart_Init  
  4. void Init_watchdog(void); // WatchdogInit  
  5. void Init_AD(void);       // ADInit  
Code:
  1. #include  <msp430x14x.h>   
  2. #include  <string.h>   
  3. #include  "Init.h"   
  4. #define Num_of_Results   32   
  5.   
  6. static uint results[Num_of_Results];    //保存ADC转换结果的数组                                                     // is not used for anything.  
  7. void Trans_val(uint Hex_Val);      //AD电压转换  
  8. void Send_data(char *data);  //Uart_send_function  
  9. void Init(void);          // Main_Init  
  10.   
  11. uchar shuzi[] = {"0123456789."};  //字码转换  
  12.   
  13. /******************************************* 
  14. 函数名称:主函数  
  15. 功    能:主函数  
  16. 参    数:无         
  17. 返回值  :无  
  18. ********************************************/  
  19. int main(void)   
  20. {     
  21.   Init();   //主初始化程序   
  22.   _EINT();   
  23.   LPM0;   
  24.   while(1);   
  25.   return 0;   
  26. }   
  27. /******************************************* 
  28. 函数名称:初始化函数  
  29. 功    能:初始化模块调用  
  30. 参    数:无         
  31. 返回值  :无  
  32. ********************************************/  
  33. void Init(void)   
  34. {   
  35.     Init_watchdog();     // WatchdogInit  
  36.     Init_uart(9600);     // Uart_Init   
  37.     Init_AD();           // AD_Init  
  38. }   
  39. /******************************************* 
  40. 函数名称:串口发送数据  
  41. 功    能:发送数据  
  42. 参    数:字符串        
  43. 返回值  :无  
  44. ********************************************/  
  45. void Send_data(char *data)   
  46. {   
  47.     unsigned char i;    
  48.     for(i=0;i<strlen(data)+1;i++)   
  49.     {   
  50.      TXBUF1=data[i];   
  51.      while((UTCTL1&TXEPT)!=TXEPT);   
  52.     }   
  53. }   
  54. /*******************************************  
  55. 函数名称:串口接收中断函数  
  56. 功    能:串口接受  
  57. 参    数:无         
  58. 返回值  :无  
  59. ********************************************/  
  60. //ISR for uart1 receive a byte.   
  61. #pragma vector=UART1RX_VECTOR   
  62. __interrupt void usart1_rx (void)   
  63. {   
  64.     if (RXBUF1 =='u')                   // 'u' received?  
  65.     {   
  66.       _NOP();   
  67.       //TXBUF1 = string1[i++];                        
  68.     }   
  69. }   
  70. /*******************************************  
  71. 函数名称:Trans_val  
  72. 功    能:将16进制ADC转换数据变换成三位10进制  
  73.           真实的模拟电压数据,并在液晶上显示  
  74. 参    数:Hex_Val--16进制数据  
  75.           n--变换时的分母等于2的n次方         
  76. 返回值  :无  
  77. ********************************************/  
  78. void Trans_val(uint Hex_Val)   
  79. {   
  80.     unsigned long caltmp;   
  81.     uint Curr_Volt;   
  82.     uchar t1,i;   
  83.     uchar ptr[4];   
  84.        
  85.     caltmp = Hex_Val;   
  86.     caltmp = (caltmp << 5) + Hex_Val;           //caltmp = Hex_Val * 33  
  87.     caltmp = (caltmp << 3) + (caltmp << 1);     //caltmp = caltmp * 10  
  88.     Curr_Volt = caltmp >> 12;                   //Curr_Volt = caltmp / 2^n  
  89.     ptr[0] = Curr_Volt / 100;                   //Hex->Dec变换  
  90.     t1 = Curr_Volt - (ptr[0] * 100);   
  91.     ptr[2] = t1 / 10;   
  92.     ptr[3] = t1 - (ptr[2] * 10);   
  93.     ptr[1] = 10;                                //shuzi表中第10位对应符号"."  
  94.     //在液晶上显示变换后的结果   
  95.     uchar result[4];   
  96.        
  97.     for(i = 0;i < 4;i++)   
  98.     {   
  99.       result[i]=shuzi[ptr[i]];   
  100.     }   
  101.     Send_data(result);   
  102. }   
  103.   
  104. /*******************************************  
  105. 函数名称:ADC12ISR  
  106. 功    能:ADC中断服务函数,在这里用多次平均的  
  107.           计算P6.0口的模拟电压数值  
  108. 参    数:无         
  109. 返回值  :无  
  110. ********************************************/  
  111. #pragma vector=ADC_VECTOR   
  112. __interrupt void ADC12ISR (void)   
  113. {   
  114.   static uint index = 0;   
  115.   
  116.   results[index++] = ADC12MEM0;               // Move results  
  117.   if(index == Num_of_Results)   
  118.   {   
  119.         uchar i;   
  120.         unsigned long sum = 0;   
  121.   
  122.         index = 0;   
  123.         for(i = 0; i < Num_of_Results; i++)   
  124.         {   
  125.             sum += results[i];   
  126.         }   
  127.         sum >>= 5;                            //除以32  
  128.            
  129.         Trans_val(sum);   
  130.   }   
  131. }   
  132.   

花了一个白天搭了下,还是挺好玩的

原创粉丝点击