| AVR BASCOM Compiler |
Showing posts with label AVR LEARNING. Show all posts
Showing posts with label AVR LEARNING. Show all posts
30 Nov 2011
AD Convertor
| AD Convertor |
Description
An analog to digital converter converts a analog input voltage into a digital value. The resolution of the converter indicates the number of discrete values it can produce. It is usually expressed in bits. For example, an ADC that encodes an analog input to one of 256 discrete values has a resolution of eight bits, 28 = 256. Most ADCs are linear, which means that they are designed to produce an output value that is a linear function of, i.e. proportional to, the input. In this example the AT Mega8 is used, it has 6 AD-converters which have a resolution of 10 bits so it has 210 = 1024 discrete values. In his example a potmeter is connected to the portC.0 of the Mega8 and a LCD module of 20x4 characters is connected to portD. The LCD module displays the values that are measured on the ADC port. The values are presented on the display as discrete values (0 to 1023), the percentage (0 to 100%) of the values and in a bargraph display.
Software
The software of this example is written in the BASCOM AVR program language. BASCOM has build in commands for reading from an ADC port, the A/D convertor of the Mega8 has to be configured first before it can read:
|
Labels:
AVR LEARNING
Blink A LED
Description
For beginners the first thing to try on a new system or in a new programming language is to print out "hello world". The equivalent for microcontrollers and other embedded systems is to blink a LED. When even the target circuit and the programmer are freshly put together it is wise to start even lower, with a blink-a-LED program written by someone else that is known to be working.
On this page you will find a 1 Hz blink-a-LED test programs for the Tiny12 and the AT2313 targets chips and circuits. You can build the circuits on a breadboard or use the AT2313 ISP board. The programs are made in assembler with AVR Studio 4 for the ATTiny12 and with the BASCOM complilor for the AT2313. Look on the AVR Programming Hardware pages for how to make a programming cable and on the AVR Programming Software pages for the software to get the hex file into your device.
Because the ports of the AVR chips can draw 20mA current, only a resistor to limit the current is neccesary to connect a LED to one of the ports. In both the circuits the LED is connected to PortB.0. In the circuit for the ATTiny12 the internal 1.2 MHz oscillator is used, because of that you don't need an external oscillator, but it can be configured also for an external crystal. The ATTiny2313 can also be used with his internal oscillator.
|
Labels:
AVR LEARNING
IIC EEPROM Interface
Description
A write operation requires a device adress bytes, two adress bytes and the data-byte. Upon receive of the adress the EEPROM sends an ACK and then clocks in the data-byte. The EEPROM sends again an ACK and the microcontrollers sends a stop-signal to terminate the write sequence.
Hardware
In this example the AT24C32 EEPROM of Atmel is connected to a AT2313 microcontroller. There is also a LCD display hooked to the microcontroller to display the data. The 24C32 has 4096 bytes of memory. The SCL and SDA lines of the EEPROM are connected to PORTD.0 and PORTD.1 of the microcontroller. Below is the schematic how it is connected. Go also here to see how you can hook up a LCD module to the microcontroller.
Software
The BASCOM-AVR compiler is used to make a program that writes and reads one byte from the EEPROM. AVR-BASCOM has several commands embedded to control the I2C bus. Below you can see the commands:
In BASCOM-AVR you first have to configure the ports you use for the SDA and SCL lines of the I2C bus. Then you send the device adress to select the EEPROM that is connected to the I2C bus. After that you send two bytes to the EEPROM to select the adress in the EEPROM to which you want to write the data. The last byte to send in a write sequence is the data byte.
Below you see the code for the example program. The program writes a byte from variable D_wr into the EEPROM at adress 0 and then reads the byte from the EEPROM at adress 0 and puts it into the variable D_rd, which is then displayed on the LCD module.
***************************************************************************** ' * Title : EEPROM 24C32.bas ' * Last Updated : 05.03.2006 ' * Target device: At90s2313, 24C32 ' * Author : www.avrprojects.net ' * Program code : BASCOM-AVR ' * Hardware req. : ' * Description : ' * This application reads and write a byte to an 24c32 EEPROM connected to an ' * AT2313 microcontroller. ' **************************************************************************** Dim D_w As Byte , D_r As Byte Config Lcdpin = Pin , Db4 = Portb.3 , Db5 = Portb.2 , Db6 = Portb.1 , Db7 = Portb.0 , E = Portb.6 , Rs = Portb.7 Config Lcd = 16 * 2 Cls Cursor Off Config Scl = Portd.0 'assign the SCl line to PORTD.0 Config Sda = Portd.1 'assign the SDA line to PORTD.1 D_w = 100 '********** write byte to EEPROM *********************************************** I2cstart 'generate start I2cwbyte &B1010_0000 'send device address I2cwbyte 0 'H adress of EEPROM I2cwbyte 0 'L adress of EEPROM I2cwbyte D_w 'data to EEPROM I2cstop 'stop condition Waitms 10 '********** read byte from EEPROM ********************************************** I2cstart 'generate start I2cwbyte &B1010_0000 'send device adsress I2cwbyte 0 'H address of EEPROM I2cwbyte 0 'L address of EEPROM I2cstart 'repeated start I2cwbyte &B1010_0001 'slave address (read) I2crbyte D_r , Nack 'read byte from EEPROM I2cstop 'generate stop Lcd "D_w= " Lcd D_w 'show byte on LCD Lowerline Lcd "D_r= " Lcd D_r End
In the picture below you can see how a write sequence looks on a oscilloscope. The upper signal is the SCL line and the lower signal is the SDA line. You can easely recognise the device adress 1010 at the start of the sequence.
| ||||||||||||||||||||||||||
Labels:
AVR LEARNING
LCD@AVR
Description
This page describes how to connect a Liquid Cristal Display to a AVR-microcontroller. The example is based on a LCD module with the Hitachi HD44780 LCD-controller.You can get these displays in various kinds, from 1 to 4 lines and from 8 to 40 characters per line. A display with 16 characters per line and 2 lines is used in this example. The display uses a power supply of 5V DC. Connecting of the display to the AVR-microcontroller is easy.
The display module has 8 data lines and 3 control lines. The 8 data lines are used to send data or instructions to the module. Instructions that can be send to the display are for example clearing the display or set the cursor. The control lines are of course used to control the display.
The 8 data lines can be used as a 4-bit bus or as a 8-bit bus. In the 4-bit bus mode the data and instructions are send by 2 groups of 4 bits after each other so the program code will be more then when the 8-bits bus is used. In this example the 4-bits bus is used, this saves 4 connections and because of that the display can be controlled by only one 8-bit port of the AVR microcontroller.
The line Enable controls the display. If it is high the data on data bus is put in the register of the display. If the line is low all the data lines are tri-state, what means that they are not connected to the microcontroller. You can see this as a switch that is open.
The line Read/Write enables reading from or writing to the display. If the R/W signal is low data can be written to the display, if it is high data can be read from the display. This is useful if you want to know if the module is ready to recieve new data.
The line Register Select selects if the data send to the display are instructions for the display or characters that have to be shown on the display.
In the table below you can see the pin assignment of the LCD module
Hardware
In the table below you can see how the LCD module is connected to the AVR-microcontroller.Because the display is used in the 4-bit mode, 4 lines of the data bus are not connected.The signal R/W has to be connected to GND so the display is always in the write mode. The pins 15 and 16 are used to connect the supply for the LED-backlight of the display, if it is present at the module. If you use 5V DC you need to place a resistor to limit the current. With a trimpotmeter of 10K connected at pin 3 (VO) of the display the contrast can be set.
Below is the schematic that shows you how to connect the LCD to the AVR microcontroller via a 10 pole flatcable with a 10 pin connector. The 10 pin connector can be plugged into the 10 pin header of the STK500 Board or the ATTiny2313 ISP Board.
The LCD module connected to the AT2313 Project Board.
Software
To control the display you can use BASCOM or Assembler. BASCOM has special commands for configuring and driving a LCD Display. In the table below you see the LCD commands and their functions.
To control the LCD display you need to implement the instructions in your program code. First you have to configure the display, which type of display you use (number of lines and number of characters on a line), to which pins of the microcontroller the lines of the LCD module are connected and which mode you use, 4-bit mode or 8-bit mode (default is 4-bit mode). Below is an example program of how to write text to the LCD display.
| ||||||||
Labels:
AVR LEARNING
Pulse Widfth Modulation Fading LED
Description
Pulse Width Modulation allows microcontrollers to dim lights, control motor speeds, fan speeds and generate analog voltages. By changing the length of the pulse, the output can be controlled. The pulse occurs at a regular frequency, the modulation frequency. The length of the pulse ratio to period time is called the duty cycle.The larger the duty cycle the higher the output is.
The AVR microcontroller can be used to generate PWM signals. The PWM signals can be generated by hardware or by software. An microcontroller like the ATTiny2313 has one hardware PWM on board of the chip. The output of the PWM signal is at the PORTB.3(OC1) pin.
The hardware PWM can be programmed by setting the timer registers. The ATTiny2313 has three timer registers that you need to set to program the PWM:
PWM with the AVR-microcontroller is a matter of comparison. If the Timer/Counter1 is running and the value of the timer is matching the value that is put in the OCR1A register, the OC1 pin changes from high to low. By changing the value of the OCR1A register the lenght of the pulse can be changed.
Hardware
The PWM output can be used to fade a LED in and out. On the picture you can see the LED on a bread-board connected to the ISP-board with a flat-cable.
| ||||||||||
Labels:
AVR LEARNING
Serial Communication RS232
Description
With serial communication you can exchange data with the AVR-microcontroller and your PC. Allmost all AVR-microcontroller have a UART (Universal Asynchronous Reciever/Transmitter) on board of the chip (accept for the AT1200 and some ATTiny microcontrollers). The data transmission between the PC and the microcontroller is serial and asynchronous, serial means that the bits are send one after the other and asynchronous means that there is no clock signal to clock in the data that is send or recieved. One byte is transmitted in 10 bits, 1 start bit, 8 data bits and one stop bit, as you can see in the figure below.
The serial data transmission has a standard that is called RS232. According to this standard a logical "0" has a voltage level between -15V and -5V and a logical "1" has a level between +5V and +15V. The AVR-microcontrolers use 5V (TTL-level) to transmit signals. So the signals needs to be converted, this can be done with the MAX232, that only needs a 5V power supply to convert the signal from TTL-level to RS232 level and reverse.
Transmission between two RS232 devices can take place with a maximum distance of 15 meters. Hardware
Below you can see the schematic of how the RS232 convertor is connected to the AT microcontroller.
Software
With BASCOM you can easily write software to communicate with the AVR-microcontroller, because BASCOM has several commands for the serial communication. Below is an example program, as you can see it takes little effort with BASCOM to implement serial communication into your microcontroller system. The program will print text in the terminal program. To test the program you can use the BASCOM terminal emulator or Hyperterminal, or my own terminal program. The settings for the COM port are: COM1,9600,N,8,1.
| ||||||||||||
Labels:
AVR LEARNING
Subscribe to:
Posts (Atom)