專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命
提供開發(fā)工具、應(yīng)用測試 完善的開發(fā)代碼案例庫分享
從全面的產(chǎn)品導(dǎo)入到強大技術(shù)支援服務(wù) 全程貼心伴隨服務(wù),創(chuàng)造無限潛能!
提供新的芯片及解決方案,提升客戶產(chǎn)品競爭力
提供最新的單片機資訊,行業(yè)消息以及公司新聞動態(tài)
十年專注單片機方案開發(fā)的方案公司英銳恩,分享CCSC rs232.c程序操作MCD2demo ADC CH0測量電壓通過rs232到PC。英銳恩現(xiàn)提供服務(wù)產(chǎn)品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。
*=============================================================================
PIC RS232收到'S'or's',返回"S:StartADC",開始測量CH0電壓,并且將數(shù)據(jù)通過RS232送出;
收到'Q'or'q',返回"Q:EndADC",停止量測CH0電壓.
============================================================================*/
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7.11 + CCS C3.18
// Use 16F877A ADC and Rs232 for MCD2-demo
//
// by LW7807@163.com
// 2005/06/23
///////////////////////////////////////////////////////////////////////////////
#include <16F877A.h> // PIC16F877 header file
#pragma device ADC=10 // 10 bits ADC
#pragma use delay(clock=4000000) // for 4Mhz crystal
#pragma fuses XT, NOWDT, NOPROTECT, NOLVP // for debug mode
#pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=MYPC)
// rs232 setting
#define DO_NOTHING 0
#define DO_START_ADC 1
#define DO_END_ADC 2
/*===========================================================================*/
void main(void)
{
int16 int_volt;
int8 str_volt[21];
float flt_volt;
int8 do_what = DO_NOTHING;
int8 tmp;
while(1){
// if rs232 get char
if(0 != kbhit())
{
tmp = fgetc(MYPC);
switch(tmp)
{
case 'S':
case 's':
printf( "S:StartADC\n" );
do_what = DO_START_ADC;
// init pic16f877a adc
setup_adc_ports(RA0_RA1_RA3_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
break;
case 'Q':
case 'q':
printf( "Q:EndADC\n" );
do_what = DO_END_ADC;
// adc off
setup_adc(ADC_OFF);
break;
default : // rs232 get other char
putc(tmp);
break;
}//end switch(tmp)
}//end if(kbhit())
switch(do_what)
{
case DO_START_ADC:
// start adc and send result to PC
int_volt = read_adc();
flt_volt = 5.0 * int_volt / 0x3ff;
sprintf(str_volt, "ADC_CH0:%1.3fV\n", flt_volt);
printf(str_volt);
printf( "delay_ms(300)\n" );
delay_ms(300);
break;
case DO_END_ADC:
// you want to do
break;
case DO_NOTHING:
// you want to do
break;
default:
break;
}//end switch(do_what)
}//end while(1)
}//end main()
附上RS232.c程序lst文件
CCS PCM C Compiler, Version 3.187, 16465
Filename: D:\myPIC\ccsc_rs232.LST
ROM used: 1091 (13%)
Largest free fragment is 2048
RAM used: 39 (22%) at main() level
55 (31%) worst case
Stack: 2 locations
*
0000: MOVLW 00
0001: MOVWF 0A
0002: GOTO 34B
0003: NOP
.................... /*=============================================================================
.................... PIC RS232收到'S'or's',返回"S:StartADC",開始測量CH0電壓,并且將數(shù)據(jù)通過RS232送出;
.................... 收到'Q'or'q',返回"Q:EndADC",停止量測CH0電壓.
.................... ============================================================================*/
.................... ///////////////////////////////////////////////////////////////////////////////
.................... // MPLAB IDE V7.11 + CCS C3.18
.................... // Use 16F877A ADC and Rs232 for MCD2-demo
.................... //
.................... // by LW7807@163.com
.................... // 2005/06/23
....................
///////////////////////////////////////////////////////////////////////////////
.................... #include <16F877A.h> // PIC16F877 header file
.................... //////// Standard Header file for the PIC16F877A device ////////////////
.................... #device PIC16F877A
.................... #list
....................
.................... #pragma device ADC=10 // 10 bits ADC
.................... #pragma use delay(clock=4000000) // for 4Mhz crystal
0334: MOVLW 3F
0335: MOVWF 04
0336: MOVF 00,W
0337: BTFSC 03.2
0338: GOTO 348
0339: MOVLW 01
033A: MOVWF 78
033B: CLRF 77
033C: DECFSZ 77,F
033D: GOTO 33C
033E: DECFSZ 78,F
033F: GOTO 33B
0340: MOVLW 4A
0341: MOVWF 77
0342: DECFSZ 77,F
0343: GOTO 342
0344: NOP
0345: NOP
0346: DECFSZ 00,F
0347: GOTO 339
0348: BCF 0A.3
0349: BCF 0A.4
034A: GOTO 43B (RETURN)
.................... #pragma fuses XT, NOWDT, NOPROTECT, NOLVP // for debug mode
....................
.................... #pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=MYPC)
.................... // rs232 setting
....................
.................... #define DO_NOTHING 0
.................... #define DO_START_ADC 1
.................... #define DO_END_ADC 2
....................
....................
.................... /*===========================================================================*/
.................... void main(void)
.................... {
034B: CLRF 04
034C: MOVLW 1F
034D: ANDWF 03,F
034E: MOVLW 06
034F: BSF 03.5
0350: MOVWF 1F
0351: MOVLW 07
0352: BCF 03.5
0353: MOVWF 1F
0354: MOVLW 19
0355: BSF 03.5
0356: MOVWF 19
0357: MOVLW 26
0358: MOVWF 18
0359: MOVLW 90
035A: BCF 03.5
035B: MOVWF 18
.................... int16 int_volt;
.................... int8 str_volt[21];
.................... float flt_volt;
.................... int8 do_what = DO_NOTHING;
*
035D: CLRF 3C
.................... int8 tmp;
....................
.................... while(1){
.................... // if rs232 get char
.................... if(0 != kbhit())
.................... {
035E: MOVLW 00
035F: BTFSC 0C.5
0360: MOVLW 01
0361: XORLW 00
0362: BTFSC 03.2
0363: GOTO 3B1
.................... tmp = fgetc(MYPC);
0364: BTFSS 0C.5
0365: GOTO 364
0366: MOVF 1A,W
0367: MOVWF 3D
.................... switch(tmp)
.................... {
0368: MOVF 3D,W
0369: MOVWF 77
036A: MOVLW 53
036B: SUBWF 77,W
036C: BTFSC 03.2
036D: GOTO 37B
036E: MOVLW 73
036F: SUBWF 77,W
0370: BTFSC 03.2
0371: GOTO 37B
0372: MOVLW 51
0373: SUBWF 77,W
0374: BTFSC 03.2
0375: GOTO 399
0376: MOVLW 71
0377: SUBWF 77,W
0378: BTFSC 03.2
0379: GOTO 399
037A: GOTO 3AC
.................... case 'S':
.................... case 's':
.................... printf( "S:StartADC\n" );
037B: CLRF 3E
037C: MOVF 3E,W
037D: CALL 004
037E: INCF 3E,F
037F: MOVWF 77
0380: MOVF 77,W
0381: BTFSS 0C.4
0382: GOTO 381
0383: MOVWF 19
0384: MOVLW 0B
0385: SUBWF 3E,W
0386: BTFSS 03.2
0387: GOTO 37C
.................... do_what = DO_START_ADC;
0388: MOVLW 01
0389: MOVWF 3C
....................
.................... // init pic16f877a adc
.................... setup_adc_ports(RA0_RA1_RA3_ANALOG);
038A: MOVLW 84
038B: BSF 03.5
038C: MOVWF 1F
.................... setup_adc(ADC_CLOCK_INTERNAL);
038D: BCF 03.5
038E: MOVF 1F,W
038F: ANDLW 38
0390: IORLW C1
0391: MOVWF 1F
.................... set_adc_channel(0);
0392: MOVLW 00
0393: MOVWF 78
0394: MOVF 1F,W
0395: ANDLW C7
0396: IORWF 78,W
0397: MOVWF 1F
.................... break;
0398: GOTO 3B1
.................... case 'Q':
.................... case 'q':
.................... printf( "Q:EndADC\n" );
0399: CLRF 3E
039A: MOVF 3E,W
039B: CALL 014
039C: INCF 3E,F
039D: MOVWF 77
039E: MOVF 77,W
039F: BTFSS 0C.4
03A0: GOTO 39F
03A1: MOVWF 19
03A2: MOVLW 09
03A3: SUBWF 3E,W
03A4: BTFSS 03.2
03A5: GOTO 39A
.................... do_what = DO_END_ADC;
03A6: MOVLW 02
03A7: MOVWF 3C
....................
.................... // adc off
.................... setup_adc(ADC_OFF);
03A8: MOVF 1F,W
03A9: ANDLW 38
03AA: MOVWF 1F
.................... break;
03AB: GOTO 3B1
.................... default : // rs232 get other char
.................... putc(tmp);
03AC: MOVF 3D,W
03AD: BTFSS 0C.4
03AE: GOTO 3AD
03AF: MOVWF 19
.................... break;
03B0: GOTO 3B1
.................... }//end switch(tmp)
.................... }//end if(kbhit())
....................
.................... switch(do_what)
.................... {
03B1: MOVF 3C,W
03B2: MOVWF 77
03B3: MOVLW 01
03B4: SUBWF 77,W
03B5: BTFSC 03.2
03B6: GOTO 3BF
03B7: MOVLW 02
03B8: SUBWF 77,W
03B9: BTFSC 03.2
03BA: GOTO 43E
03BB: MOVF 77,F
03BC: BTFSC 03.2
03BD: GOTO 43F
03BE: GOTO 440
.................... case DO_START_ADC:
.................... // start adc and send result to PC
.................... int_volt = read_adc(); flt_volt = 5.0 * int_volt / 0x3ff;
03C8: MOVF 22,W
03C9: MOVWF 3F
03CA: MOVF 21,W
03CB: MOVWF 3E
03CC: GOTO 049
03CD: CLRF 41
03CE: CLRF 40
03CF: MOVLW 20
03D0: MOVWF 3F
03D1: MOVLW 81
03D2: MOVWF 3E
03D3: MOVF 7A,W
03D4: MOVWF 45
03D5: MOVF 79,W
03D6: MOVWF 44
03D7: MOVF 78,W
03D8: MOVWF 43
03D9: MOVF 77,W
03DA: MOVWF 42
03DB: GOTO 069
03DC: MOVF 77,W
03DD: MOVWF 3E
03DE: MOVF 78,W
03DF: MOVWF 3F
03E0: MOVF 79,W
03E1: MOVWF 40
03E2: MOVF 7A,W
03E3: MOVWF 41
03E4: MOVWF 45
03E5: MOVF 79,W
03E6: MOVWF 44
03E7: MOVF 78,W
03E8: MOVWF 43
03E9: MOVF 77,W
03EA: MOVWF 42
03EB: CLRF 49
03EC: MOVLW C0
03ED: MOVWF 48
03EE: MOVLW 7F
03EF: MOVWF 47
03F0: MOVLW 88
03F1: MOVWF 46
03F2: GOTO 0E2
03F3: MOVF 7A,W
03F4: MOVWF 3B
03F5: MOVF 79,W
03F6: MOVWF 3A
03F7: MOVF 78,W
03F8: MOVWF 39
03F9: MOVF 77,W
03FA: MOVWF 38
.................... sprintf(str_volt, "ADC_CH0:%1.3fV\n", flt_volt);
*
035C: CLRF 20
*
03FB: MOVLW 23
03FC: MOVWF 20
03FD: CLRF 3E
03FE: MOVF 3E,W
03FF: CALL 022 delay_ms(300);
0436: MOVLW 02
0437: MOVWF 3E
0438: MOVLW 96
0439: MOVWF 3F
043A: GOTO 334
043B: DECFSZ 3E,F
043C: GOTO 438
.................... break;
043D: GOTO 441
.................... case DO_END_ADC:
.................... // you want to do
.................... break;
043E: GOTO 441
.................... case DO_NOTHING:
.................... // you want to do
.................... break;
043F: GOTO 441
.................... default:
.................... break;
0440: GOTO 441
.................... }//end switch(do_what)
....................
.................... }//end while(1)
0441: GOTO 35E
.................... }//end main()
....................
0442: SLEEP
Configuration Fuses:
Word 1: 3F79 XT NOWDT NOPUT NODEBUG NOPROTECT BROWNOUT NOLVP NOCPD NOWRT