Simple c-programs for input and hardware PWM.
Here are two programs that use the A/D converter in the AVR tiny 45
in a simple way. The first does an analog read every 1/2
second and sends the most significant byte of the result through the
serial output at 9600 baud. The second reads an analog input
and uses the result to set a PWM duty cycle, using the hardware PWM.
Refer to comments in the programs, and to the AVR tiny45 datasheets
for explanations. Note that the code is transferrable to other
chips with little or no alteration.
1. Here is a simple program and makefile to make an A/D
conversion and read the most significant byte to serial.
Comments:
- It uses Neil's bitwise code for sending serial. It's
possible that it will be necessary to tweak the delay time
if this doesn't work with a particular chip. Internal
clock calibration is only good to a few percent.
- It throws away the two least significant bits of the reading,
effectively dividing by four and losing the remainder, so that
the answer ranges from 0 to 255, rather than 0 to 1023.
- Read the results using any serial method in which you can
interpret the bytes as numbers: Python serial, Coolterm,
many others.
2. Here is a program that alters PWM in response to the A/D
read.
Comments: