完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
亲爱的各位,我在寻找能显示从0到1024改变值的样例代码,然后把那个值转换成电压。该值必须是16位寄存器。低字节存储在一个地址和另一个高位字节。有示例代码吗?
以上来自于百度翻译 以下为原文 Dear all, I am looking for sample code which could display the change the value from 0-1024 then convert that value into voltage . The value must be 16bit register. lower byte store in one address and higher byte in another. Is there example code. |
|
相关推荐
16个回答
|
|
当然,你自己已经取得了什么成就?硬件可以吗?
以上来自于百度翻译 以下为原文 Sure there is What have you already achieved by yourself? Hardware is ok? |
|
|
|
我使用RBO和Rb1连接到10K锅。我想看到价值的变化。我的代码看起来像下面我看不到变化是价值
以上来自于百度翻译 以下为原文 i m using RBO & Rb1 connected to 10k pot. I would like to see change in value. my code look like below i cant see change is value unsigned int curr1; float curr[27] = 0; bit select = 0,Auto_flag = 0, curr1_min=0, curr2_min=0, curr3_min=0, curr4_min=0, curr5_min=0, curr6_min=0, curr7_min=0, curr8_min=0, curr9_min=0, curr10_min=0; void PIN_MANAGER_Initialize(void) { /** LATx registers */ LATA = 0x00; LATB = 0x00; LATC = 0x00; /** TRISx registers */ TRISA = 0x00; TRISB = 0x00; TRISC = 0x80; /** ANSELx registers */ ANSELC = 0x00; ANSELB = 0x3F; ANSELA = 0x00; /** WPUx registers */ WPUE = 0x00; WPUB = 0x00; WPUA = 0x00; WPUC = 0x00; /** ODx registers */ ODCONA = 0x00; ODCONB = 0x00; ODCONC = 0x00; bool state = (unsigned char)GIE; GIE = 0; PPSLOCK = 0x55; PPSLOCK = 0xAA; PPSLOCKbits.PPSLOCKED = 0x00; // unlock PPS RC6PPS = 0x09; //RC6->EUSART:TX; RXPPSbits.RXPPS = 0x17; //RC7->EUSART:RX; PPSLOCK = 0x55; PPSLOCK = 0xAA; PPSLOCKbits.PPSLOCKED = 0x01; // lock PPS GIE = state; } float Adc12_Cha(unsigned char val) { unsigned char adc_hbit,adc_lbit; unsigned long adc_temp,adc_temp0,adc_val; ADCON2=0x95; //ADFM=1,adc_clk=FOSC/4 adc_temp0=0; for(unsigned char j=0; j<10; j++) { ADCON0=val<<2; ADON=1; __delay_ms(5); GODONE =1; while(GODONE==1); adc_hbit=ADRESH; adc_lbit=ADRESL; adc_temp = adc_lbit + (256*adc_hbit); adc_temp0+=adc_temp; } adc_val=adc_temp0/10; return adc_val; } void ADC_Manipulation() { curr1 = (int)(curr[0]); if ((curr1<=102) ||(curr1>=502 && curr1<=514)||(curr1>=922)) { curr1=0; curr1_min=0; } else if (curr1>514) { if((curr1>514)||(curr1<540)) curr1= (((curr1-512)*74)/100); else curr1=(((curr1-512)*74)/100); curr1_min=0; } else { if((curr1>102)||(curr1<150)) curr1= (((511-curr1)*74)/100); else curr1=(((510-curr1)*74)/100); // curr1_min=1; } } void main(void) { while (1) { curr[0] = Adc12_Cha(10); ADC_Manipulation(); __delay_ms(100); } } |
|
|
|
您需要将这些引脚设置为输入,以及模拟能力(以及其他数字)。此外,我将避免所有的平均值,直到您确信可以正确读取ADC。
以上来自于百度翻译 以下为原文 You need to set those pins as inputs, and analog capable (and the others, digital). also, I'd avoid all of that averaging until you're sure you can read ADC correctly. |
|
|
|
我在这里附上了配置细节。我使用RB0&AMP;RB1作为模拟端口。如果我被配置为输入,它将不会编译。甚至它的警告它不会更新到芯片。
以上来自于百度翻译 以下为原文 I have attached config details here. I am using RB0 & RB1 as analog ports. If i am config as input it wont compile at all. even its warning it wont update in to chip. unsigned int ADCRead(unsigned char ch) { if(ch>13) return 0; //Invalid Channel ADCON0=0x00; ADCON0=(ch<<2); //Select ADC Channel ADON=1; //switch on the adc module GODONE=1; //Start conversion while(GODONE); //wait for the conversion to finish ADON=0; //switch off adc return ADRES; } void main(void) { unsigned char c=0; ADCON2=0b10001010; // ANSELH=0X3F; while (1) { val=ADCRead(12); __delay_ms(100); } //} } Attached Image(s) |
|
|
|
|
|
|
|
这是MCC生成的文件。在那里我被配置为输入或输出。在MCC中,如果配置为输入和选择,如先前线程中显示的那样,则会出现错误。我不知道如何解决这个问题。我试着在MCC GeAccess文件中更改PIN配置。它允许你改变,但是价值没有变化。
以上来自于百度翻译 以下为原文 This is mcc generated file. Where i am config as input or output. In mcc if config as input & selected as Analog as show in previous thread i get an error. I dont know how to resolve this. i tried changing pin config in mcc genrated file. It allow you to change but there is no change in value. |
|
|
|
我的主要挂起初始化和这个功能我从PIC18F2420复制,其中有寄存器ADCON2在这个我没有ADCON2ADCON寄存器不同于PIC18F24K40&PIC18F2420i AM没有得到PIC16F86的例子,因为引脚和ADC配置设置相同。
以上来自于百度翻译 以下为原文 my main suspection intialization and this function The code i copied from PIC18F2420 Where there is register ADCON2 in this i dont have adcon2 ADCON register are differ from PIC18F24k40 &PIC18F2420 I am not getting example for PIC16F886 since PINOUT & ADC config setting are same. unsigned int ADCRead(unsigned char ch) { if(ch>13) return 0; //Invalid Channel ADCON0=0x72; // ADCON0=(ch<<2); //Select ADC Channel ADON=1; //switch on the adc module GODONE=1; //Start conversion while(GODONE); //wait for the conversion to finish ADON=0; //switch off adc return ADRES; } |
|
|
|
|
|
|
|
你不能通过将它连接到RB0和RB1来测量锅的电阻。这里有一种可能性:将锅的一端连接到Vdd(PIC电源电压)。将另一端连接到VSS(接地)。将锅的擦拭器连接到PIC管脚上,比如说RB0。现在,坚持MCC:注意MCC管脚栅中的GPIO选择是用于数字输入和输出,所以不要尝试在那里选择RB0。这是获得模拟输入的方法:激活MCC资源管理窗口中的ADCC模块。设置适当的TAD。对于我的例子,我运行的芯片在64兆赫,所以它需要Fosc / 64给予1美元的TAD。参见MCCU-ADCC附件。如果您错误地选择了参数太短的TAD,则通知窗口中将有一个重要的提示(应该是警告)。请参阅脚注。现在您可以在PIN管理器网格中选择ADCC选择。选择RB0作为ANX输入。现在,查看MCC资源管理窗口上的PIN模块窗口。参见McCuPiNoMead模块附件,您可以看到与PIN RB0相关联的默认名称ChhannLeNANB0。如果你愿意,你可以把这个名字改成你喜欢的任何东西。使用该名称作为任何引用MCC生成函数的参数或基名称。现在,单击“资源管理”窗口的“生成”按钮后,现在是查看MCC生成文件ADCC .H中所描述的函数的时候,并在ADCC。要知道下一步该怎么做。DaveFootnote,有些人发现它是一个节省时间的工具,在使用MCC生成的文件来实现应用程序之前读取数据表。
以上来自于百度翻译 以下为原文 You can't measure the resistance of a pot by connecting it to RB0 and RB1. Here's a possibility: Connect one end of the pot to Vdd (PIC Supply voltage). Connect the other end to Vss (Ground). Connect the wiper of the pot to a PIC pin, say RB0. Now, sticking with MCC: Note that the GPIO selections in the MCC Pin Grid are for Digital Inputs and Outputs, so do NOT try to select RB0 there. Really. Here's the way to get an Analog input: Activate the ADCC module in the MCC Resource Management window. Set up appropriate TAD. For my example, I'm running the chip at 64 MHz, so it takes Fosc/64 to give a 1 us TAD. See the MCC_ADCC attachment. If you erroneously select parameters that give TAD that is too short, there will be a significant Hint (should be a Warning) in the Notifications window. See Footnote. Now you have ADCC selection possibilities in the Pin Manager Grid. Select RB0 to be the ANx input. Now, look at the Pin Module window on the MCC Resource Management window. See the MCC_PIN_MODULE attachment You see the default name, channel_ANB0, associated with pin RB0. If you want to, you can change that name to anything that suits your fancy. Use that name as the argument or base name to any of the MCC-generated functions that refer to a channel number. Now, after clicking on the Generate button of the Resource Management window, it's time to look at the functions described in mcc generated files adcc.h and iimplemented in adcc.c to try to get a clue as to what to do next. Regards, Dave Footnote: Some people have found it to be a productivity time saver to read the Data Sheet before trying to implement the application using MCC-generated files. Attached Image(s) |
|
|
|
对于我来说,我可以连接任何模拟端口。让我知道我需要使用哪个模拟端口来工作。我在MCC和Gen等级文件中使用ADCC功能。我附上了MCC文件供参考。链接使用它的ADCC C,但是没有信息如何使用它。
C配置(3.11 KB)-下载9次 以上来自于百度翻译 以下为原文 For me i can connect any analog port. Let me know which analog port i need to use to make it work. I have used ADCC function in MCC and Gen rated file . i have attached MCC file for reference. the link attached in that its used adcc.c but dont have info how to use it. void main(void) { // Initialize the device SYSTEM_Initialize(); while (1) { //val=ADCRead(12); val=ADCC_GetSingleConversion(10); //curr[0] = Adc12_Cha(10); //ADC_Manipulation(); __delay_ms(100); } //} } Attached Image(s) Attachment(s) adcc.c (8.15 KB) - downloaded 8 times pinconfig.c (3.11 KB) - downloaded 9 times |
|
|
|
对于我来说,我可以连接任何模拟端口。让我知道我需要使用哪个模拟端口来工作。你可以使用任何一个端口。他的观点是把雨刷连接到一个模拟输入,你不使用两个模拟输入。
以上来自于百度翻译 以下为原文 For me i can connect any analog port. Let me know which analog port i need to use to make it work. You can use any SINGLE port. His point was that you connect the wiper to one analog input, you don't use two analog inputs. |
|
|
|
我用了StudentCompanionI的例子代码做了设置显示。我已经使用了AN&AMAN1端口编码。它仍然显示0000没有变化的价值。我用了2x10K锅。一端给5V另一个给GND。给ANO和AN1的输出都不能读取输出。我使用串行监视器显示值转换值 以上来自于百度翻译 以下为原文 I have used the example code from StudentCompanion I have done setting shown. I have used ANO & AN1 port coding. it Still display 0000 there is no change in value. i used 2X10k pot . one end given to 5v another to gnd . Output given to ANO& AN1 Both could not able read output. I am using Serial Monitor to display the value convertedValue void Serial_1_Send_byte(uint8_t trbuf1) { TX1REG = trbuf1; while (0 == PIR3bits.TXIF); //while (!TX1IF); } void Send_string_uart1(const unsigned char *string) { unsigned char i = 0; do { TX1REG = string[i++]; while (0 == TX1STAbits.TRMT); } while (string != ' |