前面已经分享了我的界面的设计,现在分享一下我的测试代码。
示波器的数字通道用串口接收数据,并绘制对应的波形。
- unsigned char uart_rx_buff[9] = {
- 0x5A, 0x5A,
- 0x00,
- 0x00, 0x00,
- 0x00, 0x00,
- 0x00, 0x00,
- };
- unsigned char uart_tx_buff[3] = {
- 0x5A, 0xA5,
- 0x00
- };
- unsigned char uart_rx_cnt = 0;
- unsigned char uart_tx_cnt = 0;
- unsigned char uart_rx_num = 0;
- unsigned char uart_tx_num = 0;
- unsigned char uart_rx_finish = 0;
- unsigned char uart_tx_finish = 0;
- void UART2_Init(unsigned long bdt)
- {
- unsigned short tmp;
- tmp = 1024 - FOSC / 64 / bdt;
- MUX_SEL |= 0x40; //复用选择口, 选择复用为UART2, 接口引出到 P0.4、P0.5
- SetPinOut( 0, 4 );
- SetPinIn( 0, 5 );
-
- SCON2 = 0x50; //0011 0000 ,即将SM2,REN置1,SM2:只有当接收到第9位数据(RB8)为1时,才把接收到的前8位数据送入SBUF,且置位RI 发出中断申请 ,即发送方式为8N1 ,
- ADCON = 0x80; //波特率发生器选择,要使用 SREL0H:L 自行设置
-
- SREL2H = (unsigned char)( tmp >> 8 );
- SREL2L = (unsigned char)tmp;
- ES2 = 1;
- EA = 1;
- }
- void UART2_ISR_PC(void) interrupt 4
- {
- EA = 0; //中断总控制位 关闭
- if( RI2 == 1 ) //接收中断标志位 该位由硬件置位 ,软件清零
- {
- RI2 = 0;
- uart_rx_buff[ uart_rx_cnt ] = SBUF2;
- switch( uart_rx_cnt )
- {
- case 0:
- if( uart_rx_buff[ uart_rx_cnt ] == 0x5A )
- {
- uart_rx_cnt ++;
- }
- else
- {
- uart_rx_buff[ uart_rx_cnt ] = 0;
- uart_rx_cnt = 0;
- }
- break;
- case 1:
- if( uart_rx_buff[ uart_rx_cnt ] == 0x5A )
- {
- uart_rx_cnt ++;
- }
- else
- {
- uart_rx_buff[ uart_rx_cnt ] = 0;
- uart_rx_cnt = 0;
- }
- break;
- case 2:
- uart_rx_num = uart_rx_buff[ uart_rx_cnt ];
- uart_rx_cnt ++;
- break;
- default:
- uart_rx_cnt ++;
- if( uart_rx_cnt >= ( uart_rx_num + 3 ) )
- {
- uart_rx_cnt = 0;
- /* 处理波形 */
- uart_rx_finish = 1;
- }
- break;
- }
- }
- if( ti2 == 1 ) //发送中断标志位 该位由硬件置位 ,软件清零
- {
- if( uart_tx_cnt < uart_tx_num )
- {
- SBUF2 = uart_tx_buff[ uart_tx_cnt ];
- uart_tx_cnt ++;
- }
- else
- {
- uart_tx_cnt = 0;
- uart_tx_finish = 1;
- }
- TI2 = 0;
- }
- EA = 1; //中断总控制位 关闭
- }
- void UART2_Sendbyte( unsigned char dat)
- {
- while( TI2 == 1 );
- SBUF2 = dat;
- }
- void UART2_String(unsigned char * dat , unsigned char len)
- {
- unsigned char i;
- for( i = 0; i < len; i ++ )
- {
- while( TI2 == 1 );
- SBUF2 = *(dat+i);
- }
- }
复制代码
模拟通道使用AD1、AD2、AD3读取外面电压并绘制对应的波形。
- #define ADC_BASE_ADDR ( 0x32 )
- #define ADC_CH0 ( 0x00 )
- #define ADC_CH1 ( 0x01 )
- #define ADC_CH2 ( 0x02 )
- #define ADC_CH3 ( 0x03 )
- #define ADC_CH4 ( 0x04 )
- #define ADC_CH5 ( 0x05 )
- #define ADC_CH6 ( 0x06 )
- #define ADC_CH7 ( 0x07 )
- unsigned short adc_read( unsigned char chn )
- {
- unsigned short result;
- sys_read_vp( ADC_BASE_ADDR + chn, (unsigned char *)&result, 1 );
- return result;
- }
- void scop_ai_read( unsigned short * val )
- {
- sys_read_vp( ADC_BASE_ADDR + ADC_CH1 , (unsigned char *)&val, 3 );
- }
复制代码
绘制波形借用了一位大佬的代码(忘记是谁的了,如果大佬看见了可以留言,我会注明出处)。
- //绘制动态曲线
- //chart_id:曲线通道id,范围[0,7],系统总共支持8个曲线通道,每个曲线通道占用2K的字空间,
- // 总缓冲区范围:0x1000-0x4FFF,没有使用的曲线缓冲区可以做其他用途
- //y:写入某一个数据点的数据值
- void sys_write_chart(u8 chart_id,u16 y)
- {
- #define CHART_ADDR 0x0310 //动态曲线功能所对应的系统变量接口地址
- #define CHART_NUM 1 //一次性写入几个曲线通道的数据,我们只写入chart_id指定的曲线通道,即1个
- #define POINT_NUM 1 //一次性写入多少个数据点
-
- //构建命令
- u8 chart_cmd[6+POINT_NUM*2] = {0x5A,0xA5,CHART_NUM,0x00};
- chart_cmd[4] = chart_id;//曲线通道id
- chart_cmd[5] = POINT_NUM;//一次性写入多少个数据点
- chart_cmd[6] = (u8)(y>>8);//数据点值
- chart_cmd[7] = (u8)(y&0xff);
-
- //发送命令
- sys_write_vp(CHART_ADDR,chart_cmd,3+POINT_NUM);
- }
复制代码
|