-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ADC Usage on QFN variant of CH32V003 #110
Comments
I switched the sampling channel to A0 (PA_2), and from the serial output, it works fine. It is important to note the pins of the external crystal of PA2.
|
Tianpei, thank you, but that code still just gives me 0 data outputs. I have a 5K Ohm Potentiometer between 3V3 and GND, and the wiper going to PA2, I also tried PD4 -- still Zero's. EDIT: What do you mean by "003 is PA_2" |
Have you tried A2 (not PA2) to see what it returns. In issue #94 someone else reported problems with ADC. On the SOP8 package using A2 works for me. With "003 is PA_2" @TianpeiLee probable meant to say that on the CH32V003 analog pin A0 maps to pin PA_2. |
Ready to break down in tears here. A2 is one of the few pins I didn't break out, and because it's the QFN variant, it's virtually impossible to get a hold of! |
Hmmm... perhaps there is. I don't have experience in that area. I did notice you don't call pinMode() in your setup(). In my code I use this: Perhaps you can try too, to see if it makes a difference? Edit: @TianpeiLee has a different call in his example: |
Current code.
All Zero's. Changed to try PD3 and PD4 - no luck. Zero's all round. all day long. |
SOLVED IT. words cannot describe how frustrated I am. How mad I am, and also how grateful I am to this community. Thank you folks. |
Wow, that sounds quite frustrating indeed. Reminds me of the time when I have SPI working on the V003. |
or just override those defines in your INO file would be a start. |
some more salt in the wound.
|
I'm documenting my hunting here.
I'm getting close here... I can feel it. |
Good going. Looking at the other issues regarding ADC, I think you're not the only one. I spent quite some time to get reading VCC by using PADC_VREF working and only managed that by butchering some original code. See this branch of my CH32 fork. From what I read in issue #94, it seemed pins used for oscillator may behave differently. Somewhere deep down there may be some logic. (While getting I2C slave working I found the realtime debugger to be crucial in gaining some understanding). BTW. Different type of pin names can obfuscate things even more, e.g. A2 / PA2 / PIN_A2. |
Hello folks, I have enabled the ADC in the variant .h file, and I've now set the clock source to 48MHz HSI (assuming HSI = internal?) On A3 (PD2) I now get values. (connecting to VCC I get 1024, and GND = 0) My ADC clearly works, but when A0 is selected. |
Another day, another update! I downloaded this codebase and overwrote the 1.0.4 release with it. (July 8th 2024) Regardless, I went to the appropriate system_ch32v00x.c and uncommented the 48MHz HSI clock source. Once again, I'm left thinking that the mux is to blame here. /Dafydd |
The default function of PA1 and PA2 is the IO port, and the floating input in the default state of the IO port can also be sampled to obtain the ADC value. Theoretically, when using the internal HSI, it can get the value via channel 1 and channel 0 of ADC without doing anything to this IO port. |
@TianpeiLee Thank you for the response. Do you have a binary code that I can use to download to the device, so I can work out if it's a HW issue or a toolchain issue? |
build.zip |
I have been battling the ADC this week and have finally resolved the issue. It is not exactly related to the specific topic (QFN variant) but could well be relevant. My setup is:
Problem: I could not get anything sensible from the analogue input port despite looking at about 3 different examples (BTW I found them overly complicated - using timers to trigger, using interrupts etc.) A basic example should just do a standard software read IMHO). Also tried another pin (PC4/A2) - results were better but quite a significant amount of "noise" on the output. Solution: The default startup code is designed to run on the evaluation board which uses an external crystal attached to PA1/PA2 of the CH32V003F4P6 device. There is a set of defines at the top of system_ch32v00x.c (under User folder) which configures this. For the EVB, SYSCLK_Freq_48MHz_HSE is used but for any other variant NOT using an external crystal, you would need to change it to one of the ones ending in "HSI". My guess is that this initial config gets "locked" and so if you try writing the bit in the AFIO register later that controls PA1/PA2 use for the crystal, or even change the firmware with the programmer, it is not effective until you power cycle the device. You have to compile with the right define, program the device, then power cycle. The other point to note is that I noticed some really odd waveforms on the A2 pin during the process of trying to work out what was going on. I had used PD6 connected to pin 1 (also used for PA1 on the 8-pin device) as an output to an LED. The signal sent to PD6 (flashing the LED) was superimposed on the analog input pin which explained the erratic (noisy) readings I was getting on A2. Suffice it to say that once I stopped the firmware trying to use PA1/PA2 for an external crystal, my woes have disappeared and the ADC is reading very nicely. I hope this helps someone :) |
@amgrays - Excellent work! I admire your persistance in figuring out the root cause of your issue. FYI: in later versions of this core the clock selection is done in the IDE menu. I ran into clock issues months ago when I noticed that delay was off a factor of two when ran on bare chips. To fix that issue was one of my first priorities and a PR for that has been merged (but maybe not in release 1.0.4). |
Folks,
I'm trying my best to get the ADC to work, and to simply write it over serial.
`uint16_t adc = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Ready");
}
void loop() {
// put your main code here, to run repeatedly:
adc = analogRead(PA2);
Serial.print("ADC value is :");
Serial.println(adc);
delay(500);
}`
I've done all the variants mentioned on this group, and enabled the ADC in the variant_CH32V003F4.h file, but I still get a value of zero.
I feel like I'm going nuts here.
The text was updated successfully, but these errors were encountered: