Skip to content
Snippets Groups Projects
Unverified Commit 1d8bbe3e authored by CNLohr's avatar CNLohr Committed by GitHub
Browse files

Merge pull request #28 from emeb/master

Added README files to some examples
parents 54343c0f 377859e0
No related branches found
No related tags found
No related merge requests found
# Polled ADC example
This example code demonstrates the use of the CH32V003 on-chip ADC in polled mode
wherein a single analog input channel is configured on GPIO and then digitized
under software control. When a sample is needed the ADC is started and the CPU
busy-waits until the conversion is complete before reading the result.
This is just about the simplest and easiest use-case for the ADC and wastes a lot
of CPU cycles but it does show the basics of doing conversions in a situation
where high performance is not needed.
## Use
Connect an analog voltage source that varies between 0V and 3.3v to GPIO pin PD4
and a 3.3V logic level serial terminal to pin PD5. The 10-bit result will be
printed periodically on the serial output.
# I2C OLED demonstration
This example code demonstrates use of the CH32V003 I2C peripheral with an SSD1306
OLED (128x32 pixel type). It provides a generic I2C port initialization and
transmit (write-only) low level interface and a high-level graphics driver with
pixels, lines, circles, rectangles and 8x8 character font rendering. Out of the
box this demo cycles through a few different graphic screens to test out the
various drawing primitives.
https://user-images.githubusercontent.com/1132011/230734071-dee305de-5aad-4ca0-a422-5fb31d2bb0e0.mp4
## Use
Connect an SSD1306-based OLED in I2C interface mode to pins PC1 (SCL) and PC2 (SDA)
of the CH32V003 with proper I2C pullup resistors and observe the various graphic
images that cycle at approximately 2 second intervals.
Note - I used an Adafruit 0.91" 128x32 OLED breakout (stock #4440) for my testing
and found that the built-in 10k pullup resistors were too weak for reliable I2C
bus transactions. I had to add 2.2k resistors to the SCL and SDA pads to allow
proper operation.
# Systick IRQ demonstration
This example shows how to set up the built-in Systick IRQ to generate periodic
interrupts for timing. Many bare-metal and RTOS based embedded applications will
use the Systick IRQ for timing, periodic housekeeping and task arbitration so
knowing how to set that up is useful.
Note that this example is *NOT* compatible with the Delay_Ms() function that's
part of the ch32v003fun library - that function uses the Systick counter for
doing busy-wait delays and will interfere with its use in generating predictable
IRQs. Do not use the built-in Delay_Ms() and related functions when using Systick
for IRQs.
Note also the use of the `__attribute__((interrupt))` syntax in declaring the
IRQ handler. Some of the IRQ examples from the WCH HAL library have slightly
different syntax to make use of a fast IRQ mode but which is not compatible with
generic RISC-V GCC so that feature is not used here.
# Use
Connect LEDs (with proper current limit resistors) to GPIO pins C0, D0, D4 and
a 3.3V logic level serial terminal to PD5. The LEDs will flash and an incrementing
count will be printed to the serial port at rates that are controlled by the
Systick IRQ.
file tim1_pwm.elf
target extended-remote localhost:3333
# Demonstration of PWM using Timer 1
This example shows how to set up Timer 1 (the Advanced Control Timer) to generate
Pulse-Width Modulation (PWM) on two output pins. PWM is frequently used to do
variable brightness on LEDs or for digital-to-analog conversion when combined with
a suitable lowpass filter.
## Use
Connect GPIO pins PD0 and PC4 to LEDs (with appropriate current limiting) and
observe that they fade with different cycles.
#!/bin/bash
# before running this you should start OOCD server
#../../../MRS_Toolchain_Linux_x64_V1.70/OpenOCD/bin/openocd -f ../../../MRS_Toolchain_Linux_x64_V1.70/OpenOCD/bin/wch-riscv.cfg
../../../MRS_Toolchain_Linux_x64_V1.70/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gdb
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment