' ********************************************************************** ' * File: uk_BASAVR_065.BAS * ' * Version: 1.1 * ' * Date: 08.07.12 * ' * Development Tools: Bascom-AVR Demo Ver. 1.11.9.1 + * ' * + AVR bootloader grifo(r) Ver. 1.2 * ' * Cards: GMM AM08 + GMM TST3 * ' * Developed by: GRIFO(r) Italian Technology * ' * via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * Author: Gianluca Angelini * ' ********************************************************************** ' Example program 065 of BASCOM AVR course. ' PWM management: it generates a D/A conversion output, based on a PWM signal, ' defined by the position of a potentiometer, acquired through A/D converter. ' The program generates a PWM signal with a fixed frequency of about 14500 Hz ' and a duty cycle changed from the potentiometer position. The generated Pulse ' Width Modulation signal, when connected to proper RC circuit, it produces an ' analog signal proportional to potentiometer position, variable in 0÷5 V range. ' The selection of the RC circuit components defines both the stability of ' generated signal and the response time in duty cycle variation following. So, ' these values change according with user requirements and load connected to ' D/A signal. ' By connecting a LED to D/A signal, its brigtness will change according to ' potentiometer variations and, as every thing is driven by software, the ' program can decide the modality. For example the LED brightness could be ' defined even from digital inputs, delays, other analog inputs, temperatures, ' parameters in memory, etc. ' The PWM signal is generated by hw from the Timer 1 section of microcontroller, ' where the compare signal connected to pin OC1A of Mini Module, reported on ' connector CN4.3 of GMM TST3, as described in the electric diagram. ' The resolution used on Timer 1 is 8 bits while the A/D section resolution is ' 10 bits: the program will adapt the different values. ' The program describes its functionalities and uses a serial console provided ' of monitor and keyboard with a fixed physical protocol at 19200 Baud, 8 Bit x ' chr, 1 Stop bit, No parity. ' This console can be another system capable to support a serial RS 232 ' communication. In order to simplify the use it can be used a PC provided of ' one COMx line, that execute a terminal emulation program as HYPERTERMINAL or ' the homonym modality provided by BASCOM AVR (see IDE Configuration). ' The program works only when the GMM AM08 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: None. ' ' 08.07.12: uk_BASAVR_065.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to coorectly use this demo program, please execute the following ' steps: ' 1) Check the availability of M8DEF.DAT file into the directory where the ' BASCOM AVR is installed, copy it if not present and then restart the IDE. ' 2) Into the window "Options | Compiler | Chip" set: ' Chip: m8def.dat ' XRAM: None ' HW Stack: 64 ' Soft Stack: 32 ' Framesize: 64 ' XRAM waitstate: disabled ' External Access Enable: disabled ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM AM08, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' 4) At the end of compilation, after the code is programmed on GMM AM08, open ' the terminal emulation window of BASCOM AVR with the option: Tools | ' Terminal emulator (Ctrl+T) and then reset or powen on the Mini Module. '************************* Compiler directives ********************************* $regfile "M8DEF.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $crystal = 7372800 ' Microcontroller crystal frequency $hwstack = 64 ' Hardware stack space $swstack = 32 ' Software stack space $framesize = 64 ' Frame space $map ' Generate debug information $baud = 19200 ' Serial communication speed: 19200 Baud ' Other parameters fixed to: 8 bit x chr ' 1 Stop bit ' No parity '******************************* Definitions *********************************** ' The resources used by program are connected as described in following table. ' !!! Note: On GMM TST3 the jumpers must be configured as below described: ' J1 N.C. ; J2 in 1-2 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3 !!! ' ' External GMM TST3 pin Z2 pin Signal Used uP ' signal resource GMM TST3 GMM AM08 GMM AM08 signal ' PWM CN4.3 30 24 PB1 OC1A OC1A ' POT CN4.4 31 25 PC1 ADC1 ADC1 ' GND CN4.17 20 14 GND - ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Signal ' PC DB9 GMM TST3 GMM TST3 GMM AM08 GMM AM08 uP ' TX 3 3 9 3 RxD RS232 PD0 ' RX 2 2 10 4 TxD RS232 PD1 ' GND 5 5 20 14 GND - ' This table shows that the connection cable between PC COM line and CN5 of ' GMM TST3 is a normal pin to pin cable or direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pinpwm Alias Portb.1 ' Bit with output signal connected to used hw PWM (compare Timer 1) Pinrx Alias Ddrd.0 ' Bit with direction signal connected to GMM AM08 Rxd Pintx Alias Ddrd.1 ' Bit with direction signal connected to GMM AM08 TxD '************************* Constants declaration ******************************* Const Dbadpwm = 2 ' Bit difference between A/D combination (10 bits) and PWM combination (8 bits) '************************* Variables declaration ******************************* Dim Dutycycle As Byte ' PWM signal Duty cycle Dim Pwmcmb As Byte ' 8 bit combination to set on PCA controller Dim Chadc As Byte ' Channel of A/D converter section Dim Cmbadc As Word ' Combination obtained from A/D converter Dim Ncnv As Byte ' A/D conversions number for average Dim Idxadc As Byte ' A/D conversions index for average Dim Avgshift As Byte ' Number of bits to shift in order to obtain conversions average Dim Sumadc As Word ' A/D conversions sum for average Dim Avgadc As Word ' Averaged combination obtained from A/D converter '************************ Subroutines declaration ****************************** Declare Sub Ini_pwm() ' Initialize Timer 1 section to generate PWM signal Declare Sub Set_pwm() ' Define duty cycle on PWM signal Declare Sub Ini_adc1() ' Initialize A/D converter section Declare Sub Getavg_adc(byval Chadc As Byte , Byval Ncnv As Byte) ' Acquire A/D converter input with average '****************************** Main program *********************************** Main: Pinrx = 0 ' Initialize signals for serial communication Pintx = 0 ' as digital inputs Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Drive D/A conversion output with A/D converter input" Print "Mount Mini Module on Z2 of GMM TST3, connect RC circuit to CN4.3 and" Print "A/D circuit to CN4.4, as described in electric diagram." Call Ini_pwm() ' Initialize PCA section that generates PWM signal Ncnv = 8 ' Conversion number for average (multiple of 2, <=64) Call Ini_adc1() ' Initialize lines and A/D converter section, used by program Do ' Begin endless loop Call Getavg_adc(1 , Ncnv) ' Acquire ADC1 channel of A/D converter with average on Ncnv conversions Shift Avgadc , Right , Dbadpwm ' Convert A/D combination in duty cycle, with a right shift of bit resolution difference Dutycycle = Avgadc Call Set_pwm() ' Set current duty cycle on PWM signal Waitms 10 ' Delay between loops Loop ' End endless loop End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' Set the duty cycle value, passed in proper global variable, on the Timer 1 ' section, that generates the PWM signal, by using even the extreme values equal ' to lines continuosly high or low. ' BASCOM AVR have specific instructions dedicated to management of hw PWM: this ' subroutines uses these instructions in place of the direct interaction with ' Timer 1 registers. ' Input: Dutycycle = duty cycle to set on PWM signal ' Output: None Sub Set_pwm() Pwmcmb = Dutycycle Xor &HFF ' Invert duty cycle value in order to set direct proportional PWM Pwm1a = Pwmcmb ' Set duty cycle with obtained combination End Sub ' Initialize the microcontroller hw section that generates a PWM signal with ' predefined frequency, and it set the minimum duty cycle, too. ' BASCOM AVR have specific instructions dedicated to management of hw PWM: this ' subroutines uses these instructions in place of the direct interaction with ' Timer 1 registers. ' The remarks of this subroutine briefly describe the performed operations, ' but other detailed information are available in mcrocontroller data sheet ' and in BASCOM on line help. ' Input: None ' Output: Dutycycle = minimum duty cycle set on PWM signal Sub Ini_pwm() ' Set Timer 1 in PWM mode, 8 bits, compare only on OC1A with counting up ' action, minimum prescaler = 1 in order to obtain the maximum frequency. Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Prescale = 1 Dutycycle = 0 ' Set minimum duty cycle -> D/A output=0V Call Set_pwm() End Sub ' Initialize resources, variables and peripheral devices used for A/D conversion ' on channel ADC1. ' Input: None ' Output: None Sub Ini_adc1() ' The used analog input (ADC1) have multifunctions and thus it must be set as ' input without pull up Ddrc.1 = 0 ' Set PortC.1=ADC1 of uP as input Portc.1 = 0 ' without pull up ' Define conversion on request, Prescaler=64 (50% of clock A/D), internal Vref=2,56V Config Adc = Single , Prescaler = 64 , Reference = Internal_2.56 Start Adc ' Enable A/D converter section End Sub ' Perform a serie of conversions on one A/D converter input, in polling modality ' and it returns the obtained 10 bits averaged combination. ' Input: Chadc = channel to convert ' Ncnv = number of conversion for average (multiple of 2, <=64) ' Output: Avgadc = combination obtained from conversion Sub Getavg_adc(byval Chadc As Byte , Byval Ncnv As Byte) ' Calculate number of bits to shift, in order to obtain averaged conversions Avgshift = 0 ' Reset number of bits to shift Idxadc = Ncnv ' Copy number of conversions for average Do Incr Avgshift ' Increase number of bits to shift Shift Idxadc , Right , 1 ' Shift copy number of conversion for average Loop Until Idxadc <= 1 ' Repeat until conversions completed Sumadc = 0 ' Clear sum of A/D converter combinations for average For Idxadc = 1 To Ncnv ' Repeat cycle for average conversion number Cmbadc = Getadc(chadc) ' Convert A/D converter channel for average Sumadc = Sumadc + Cmbadc ' Update sum of A/D converter combinations for average Next Idxadc Avgadc = Sumadc ' Obtain average from sum of A/D converter combinations Shift Avgadc , Right , Avgshift ' by shifting the sum of the calculated number of bits (=divide End Sub ' for the average conversions number) '******************* End of subroutines used by program ************************