我正在尝试编写一个接收数字(1或2)的程序,并根据用户的选择运行函数。
我有一些问题:
*该程序是为pic18处理器编写的。
我的代码:
#include <pic18.h>
#include <stdio.h>
#include "serial.h"
#include <math.h>
__CONFIG (1, OSCSEN & HS);
__CONFIG (2, PWRTEN &BORDIS & WDTDIS);
__CONFIG (3, CCP2RB3);
__CONFIG (4,LVPDIS);
float resistor();
float capacitor();
float rin=1;
float vin=15;
int count=0;
void main()
{
char choice;
float result;
init_comms();
while(1)
{
printf ("\n\r If you want to calculate resistance, please press 1. \n");
printf ("\n\r If you want to calculate capacity, please press 2. \n");
choice= getch();
if(choice=='1'){
result= resistor();
printf ("\n\r The resistance is %f Kohm \n",result);
}
else
{
result=capacitor();
printf ("\n\ The capacitance is %f uF\n", result);
}
}
}
float resistor()
{
float low=0;
float high=0;
float r=0;
float vr=0;
float mid=0;
float calc_val=0;
#asm
bsf _TRISE,0
bcf _TRISA,1
movlw 0x69
movwf _ADCON0
movlw 0x80
movwf _ADCON1
bsf _PORTA,1
movlw 0x1D
loop_1:
decf _WREG
bnz loop_1
bsf _ADCON0,2
convert_1:
btfsc _ADCON0,2
bra convert_1
#endasm
low=ADRESL;
high=ADRESH;
//calculate resistor value
calc_val=(low+(high*0x100));
mid=vin*calc_val;
vr=(mid/0x3FF);
r=rin*(vr/(vin-vr));
return r;
}
float capacitor()
{
c=1;
return c;
}
我真的很感谢你的帮助(我是一名EE学生,已经尝试过这项工作3天但仍然没有运气:()
谢谢!