' ********************************************************************** ' * File: uk_BAS51_064.BAS * ' * Version: 1.1 * ' * Date: 15.07.12 * ' * Development Tools: Bascom 8051 COMP.,IDE 2.0.14.0 + FLIP 2.4.6 * ' * Cards: GMM 5115 + 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 064 of BASCOM 8051 course. ' PWM management: it generates a D/A conversion output, continuosly increased ' and decreased, based on a PWM signal. ' The program generates a PWM signal with a fixed frequency of about 14500 Hz ' and a duty cycle continuosly changed from the minimum to maximum and after ' from maximum to minimum. The generated Pulse Width Modulation signal, when ' connected to proper RC circuit, it produces a triangle waveform analog signal ' 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. ' The PWM signal is generated by hw from the PCA section of microcontroller, ' where the channel 0 is connected to pin CEX0 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 and the generated PWM signal will ' have 256 different values of duty cycle. ' 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 8051 (see IDE Configuration). ' The program works only when the GMM 5115 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: DOWNTO. ' ' 15/07/12: uk_BAS51_064.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to correctly use this demo program, please execute the following ' steps: ' 1) Check the availability of 89C5115.DAT file into the directory where the ' BASCOM 8051 is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Misc" set: ' Register File = 89C5115.DAT ' Byte End(Hex) = A0 ' Size warning = selected at 16384 (=4000H) ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM 5115, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' Run emulator modal = not selected ' 4) At the end of compilation, after the code is programmed on GMM 5115, select ' RUN mode and open the terminal emulation window of BASCOM 8051 with the ' option: Tools | Terminal emulator (Ctrl+T) and then reset or powen on the ' Mini Module. '************************* Compiler directives ********************************* $regfile "89C5115.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $iramstart = &H0 ' Data start address on internal RAM $ramstart = &H0 ' Data start address on external RAM $ramsize = &H100 ' External RAM size $crystal = 14745600 ' Microcontroller crystal frequency $large ' Code size > 2K $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 set in following positions: ' J1 in 2-3 ; 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 5115 GMM 5115 signal ' PWM CN4.3 30 24 P1.3 ADC3 CEX0 CEX0 ' GND CN4.17 20 14 GND - ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Used up ' PC DB9 GMM TST3 GMM TST3 GMM 5115 GMM 5115 signal ' TX 3 3 9 3 RxD RS232 P3.0 ' RX 2 2 10 4 TxD RS232 P3.1 ' GND 5 5 20 14 GND 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 P1.3 ' Signal connected to used hw PWM (CEX0) Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* Const Cret = 13 ' Carriage Return ASCII code '************************* Variables declaration ******************************* Dim Dutycycle As Byte ' PWM signal Duty cycle Dim Pwmcmb As Byte ' 8 bit combination to set on PCA controller Dim Hlpf As Byte ' General purpose bit variabile '************************ Subroutines declaration **************************** Declare Sub Ini_pwm() ' Initialize Timer 1 section to generate PWM signal Declare Sub Set_pwm() ' Define duty cycle on PWM signal '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Generate D/A conversion output increasing and decreasing with PWM signal" Print "Mount Mini Module on Z2 of GMM TST3, connect RC circuit to CN4.3." Print "Current modality:" Call Ini_pwm() ' Initialize PCA section that generates PWM signal Do ' Begin endless loop ' The time duration of PWM pulse is defined by 8 bits value of the duty ' cycle inserted by user, with direct proportion: ' 0 -> minimum duty cycle = 0% -> minimum D/A signal = 0V ' : : : : : : : : : ' 127 -> medium duty cycle = 50% -> medimum D/A signal = 2,5V ' : : : : : : : : : ' 255 -> maximum duty cycle = 100% -> maximum D/A signal = 5,0V Print "increasing D/A "; ' Show D/A signal variations Printbin Cret ' Mantains visualization on same row For Dutycycle = 0 To 255 ' Loop thet generates increasing D/A signal Call Set_pwm() ' Set current duty cycle on PWM signal Waitms 2 ' Delay between each 256 increments of D/A signal Next Dutycycle Print "decreasing D/A "; ' Show D/A signal variations Printbin Cret ' Mantains visualization on same row For Dutycycle = 255 Downto 0 ' Loop that generates decreasing D/A signal Call Set_pwm() ' Set current duty cycle on PWM signal Waitms 2 ' Delay between each 256 decrements of D/A signal Next Dutycycle 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 PCA ' section, that generates the PWM signal, by using even the extreme values equal ' to lines continuosly high or low. ' Input: Dutycycle = duty cycle to set on PWM signal ' Output: None Sub Set_pwm() If Dutycycle = 0 Then ' If minimum duty cycle Pinpwm = 0 ' Force PWM output of PCA module 0 at low level Else Pinpwm = 1 ' Set PWM signal as digital input at high level, Hlpf = Pinpwm ' in order to allow setting from PCA End If Pwmcmb = Dutycycle Xor &HFF ' Invert duty cycle value in order to set direct proportional PWM Ccapm0 = &H42 ' Enable PWM on PCA module 0 Ccap0h = Pwmcmb ' Set duty cycle with obtained combination on PWM 0 End Sub ' Initialize the microcontroller hw section that generates a PWM signal with ' predefined frequency, and it set the minimum duty cycle, too. ' 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() Adcf = Adcf And &HF7 ' Set Pinpwm = P1.3 as I/O port Pinpwm = 1 ' Initialize PWM signal as digital input at high level, Hlpf = Pinpwm ' in order to allow setting from PCA Cmod = &H02 ' PCA Run in idle,Fpca/2,no interrupt (PWM frequency=Fqz/1024)=14400 Hz Ccon = &H40 ' Enable PCA timer/counter Dutycycle = 0 ' Set minimum duty cycle -> D/A output=0V Call Set_pwm() End Sub '******************* End of subroutines used by program ************************