mikrobasic pro

32
Page 1 of 32 MIKROBASIC PRO FOR PIC16F873 AHMAD MALIKI OMAR Faculty of Electrical Engineering University Technology MARA Shah Alam SELANGOR Nov 2009

Upload: zul-joey

Post on 20-Feb-2015

536 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Mikrobasic Pro

Page 1 of 27

MIKROBASIC PRO FOR PIC16F873

AHMAD MALIKI OMARFaculty of Electrical Engineering

University Technology MARAShah AlamSELANGORNov 2009

Page 2: Mikrobasic Pro

Page 2 of 27

Table of Contents1.0 Getting Started......................................................................................................................................4

1.1 How to obtain free copy of MikroBasic PRO software?.....................................................................4

1.2 How to start?.....................................................................................................................................4

1.3 How to know that the correct file is attached with the current project?..........................................4

1.4 How to set the correct PIC type and clock frequency?......................................................................5

1.5 How to set configuration bit?............................................................................................................6

1.6 How to compile a program?..............................................................................................................6

1.7 How to copy file from other project?................................................................................................7

2.0 Programming.........................................................................................................................................7

2.1 How to call delay?..............................................................................................................................7

2.2 How to use IF statement?..................................................................................................................8

2.3 What is the default mode of PORTA of PIC16F873?..........................................................................8

2.4 How to set all PORTA of PIC16F873 to digital I/O?............................................................................8

2.5 What are TRISA, TRISB and TRISC?....................................................................................................8

2.6 How to define pin with specific name?..............................................................................................8

2.7 How to declare type of variable?.......................................................................................................9

2.8 How to assign integer literals?...........................................................................................................9

2.9 How to use BUTTON statement?.......................................................................................................9

2.10 How to do increment (INC) or decrement (DEC)?..........................................................................10

2.11 How to read analog signal?............................................................................................................10

3.0 Simulation............................................................................................................................................11

3.1 How to simulate a program?...........................................................................................................11

3.2 How to know the changes of data inside SFR during simulation?....................................................11

3.3 How to simulate program step by step?..........................................................................................11

3.4 How to send high signal to pin?.......................................................................................................11

3.5 How to measure time taken from one location to another location of program?...........................11

3.6 How to force signal at certain pin or port to high or low?...............................................................11

3.7 How to display back program file after clicking start debugger the program file disappears?........12

4.0 Hardware.............................................................................................................................................13

4.1 What beginners need to know about PIC16F873?...........................................................................13

4.2 How to read very small analog signal e.g. mV?................................................................................13

Page 3: Mikrobasic Pro

Page 3 of 27

4.3 What are the components required to build a complete workable PIC system?.............................13

4.4 How to know the PIC is damage?....................................................................................................14

4.5 The PIC fails to work, why?..............................................................................................................14

4.6 What is the maximum source current from PORTs?........................................................................14

4.7 How to drive high power load using PIC?........................................................................................14

5.0 PICSTART PLUS.....................................................................................................................................14

5.1 Why PC fails to communicate with PICStart Plus?...........................................................................14

5.2 How to connect PICSTART Plus using USB port?..............................................................................15

5.3 How to download program using PICSTARTPLUS?..........................................................................15

6.0 PROGRAM EXAMPLES..........................................................................................................................16

EXAMPLE 1: BLINKING LEDS...................................................................................................................16

EXAMPLE 2: PEDESTRIAN CROSSING.....................................................................................................16

EXAMPLE 3: TRAFFIC LIGHT...................................................................................................................17

EXAMPLE 4: RUNNING LIGHT................................................................................................................18

EXAMPLE 5: STEPPER MOTOR CONTROLLER.........................................................................................19

EXAMPLE 6: SERVO MOTOR CONTROLLER............................................................................................19

EXAMPLE 7: SIGNAL FOR SQUARE WAVE INVERTER..............................................................................20

EXAMPLE 8: ANALOG TO DIGITAL CONVERTER.....................................................................................21

EXAMPLE 9: COMPARE TWO ANALOG SIGNAL......................................................................................21

EXAMPLE 10: READ SMALL ANALOG SIGNAL.........................................................................................22

EXAMPLE 11: COMPARE ANALOG SIGNAL WITH A CONSTANT.............................................................23

EXAMPLE 12: SINGLE CHANNEL MULTIPLE PULSE PWM.......................................................................24

EXAMPLE 13: TWO CHANNELS MULTIPLE PULSE PWM.........................................................................25

EXAMPLE 14: PWM WITH VARIABLE DUTY CYLE...................................................................................26

Page 4: Mikrobasic Pro

Page 4 of 27

1.0 Getting Started

1.1 How to obtain free copy of MikroBasic PRO software?

You can obtain free copy of software from mikroelektronika website. The free version is running with full features but limited up to 2K length of program. If you required to do long program then you need to buy a license. However, I think the free version is adequate for teaching purposes.

Website: http://www.mikroe.com/en/compilers/mikrobasic/pro/pic/

1.2 How to start?

Start with create new project: Project – New project – Device type PIC16F873 – Device clock 4.000 MHz – project file name {enter project name and location} – Add files to Project (click next since no file is added) – Include libraries (include All (default)) and lastly click finish. You will get quite similar window as below.

Now you can start writing your program and remember to save your file using File – Save or save as.

1.3 How to know that the correct file is attached with the current project?

Page 5: Mikrobasic Pro

Page 5 of 27

This step is to ensure that the correct file is attached with the current project. Otherwise you might compile the wrong file which make you headache later on. You might also need to detach those unwanted files from the project.

Click View – Project manager – Source. All the files (*.mbas) are listed under source. In case your required file is not listed then click source and click right button – Add files to project – click your required file. If you want to remove some of the files, then click the file and remove file from project.

1.4 How to set the correct PIC type and clock frequency?

Under View – Project setting.

Page 6: Mikrobasic Pro

Page 6 of 27

1.5 How to set configuration bit?

Setting configuration is very important. If you wrongly set the parameters, most likely your PIC will not work. Click Project – Edit Project.

Note: Select oscillator type XT if your use resonator 4 MHz.

1.6 How to compile a program?

Page 7: Mikrobasic Pro

Page 7 of 27

Click Project - Built

1.7 How to copy file from other project?

You can copy files from other project and attach with your new project. However don’t forget to enable some of the function from the library that required by the program. If the new program you paste indicate unrecognized function then most likely you need to click those function from the library.

View – Library Manager – tick required components

2.0 Programming

2.1 How to call delay?

Example:

Delay_ms(1000) ‘ Delay 1 secondDelay_us(1000) ‘ Delay 1ms

Page 8: Mikrobasic Pro

Page 8 of 27

2.2 How to use IF statement?

Example:

IF PORTA.0 = 1 THENPORTB.0 = 1

ELSEPORTB.0 = 0

END IF

2.3 What is the default mode of PORTA of PIC16F873?

PIC16F873 has three ports: PORTA, PORTB and PORTC. PORTA has analog and digital mode whereas PORTB and PORTC have digital mode only. Default mode for PORTA is analog.

2.4 How to set all PORTA of PIC16F873 to digital I/O?

main: ADCON1 = 0x07 ' Set PORTA as DIO. Please refer page 114 of datasheet TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

2.5 What are TRISA, TRISB and TRISC?

TRISA is a SFR that control operation of PORTA. The TRISB is for PORTB and so on.

Example:

TRISA = 0x00 ‘ PORTA is set as outputTRISB = 0x0F ‘ PORTB0..PORTB3 as input, PORTB4..PORTB7 as outputTRISC = 0xFF ‘ PORTC is set as input

2.6 How to define pin with specific name?

Example:

Page 9: Mikrobasic Pro

Page 9 of 27

Dim LEDA as sbit at PORTA.0

2.7 How to declare type of variable?

Here are few examples:

Dim bf as bit ‘1 bit variable: 0 and 1Dim LEDA as sbit at PORTA.0 ’1-bit size: 0 and 1Dim as word ‘ 16 bit size: 0 .. 65535Dim tmp as byte ‘ 8 bit size: 0 .. 255Dim tmpa as char ‘8 bit size: 0 .. 255Dim tmpb as short ‘ 8-bit size: -127 .. 128Dim tmpc as integer ’16-bit size: -32768 .. 32767Dim tmpd as longword ’32-bit size: 0 .. 4294967295Dim tmpe as longint ’32-bit size: -2147483648 .. 2147483647Dim tmpf as float ’32-bit size: +/- 1.17549435082 x 10-38 .. +/- 6.80564774407 x 1038

Dim tmpg as byte[30] ‘Array declarationDim msg as string[string length]const MAX as longint = 10000const MIN = 1000 ' compiler will assume word typeconst SWITCH = "n" ' compiler will assume char typeconst MSG = "Hello" ' compiler will assume string typeconst MONTHS as byte[12] = (31,28,31,30,31,30,31,31,30,31,30,31)

2.8 How to assign integer literals?

Example:

11 ‘ decimal number$11 ‘hex number0x00 ‘hex number%11 ‘binary number“Hello” ‘ message

2.9 How to use BUTTON statement?

BUTTON statement is preferred compare to PORTA.0 statement. The BUTTON statement considers debouncing effect of a real switch. If you have problem with unrecognized components in your program when using BUTTON, make sure you tick this component in the library i.e. View – Library manager.

BUTTON(port, pin, time in ms, active state)

Page 10: Mikrobasic Pro

Page 10 of 27

Example:

BUTTON(PORTA, 0, 10, 1) ‘ The push button should be connected at ‘ PORTA0 and after 10ms of signal high the‘ program will recognized as bit high.

2.10 How to do increment (INC) or decrement (DEC)?

Example:Dim dutya, dutyb as word ....Main:

Dutya = 50Dutyb = 50

While trueInc(dutya)Dec(dutyb)……

WendEnd.

2.11 How to read analog signal?

Use Adc_Read(bit) statement to read analog signal.

Example:

Dim Baca as word....Main:

ADCON1 = 0x00 ‘Set all PORTA as analog inpout. For detail please‘ refer page 114 ‘ of PIC16F873 datasheet

INTCON = 0x00 ‘ Disable interrupt TRISA = 0xFF ‘Set PORTA as input……

While trueBaca = Adc_read(0) ‘ Read PORTA0…

Page 11: Mikrobasic Pro

Page 11 of 27

…WendEnd.

3.0 Simulation

3.1 How to simulate a program?

It is a good practice to simulate your program prior downloads it into PIC. Click Run – Start Debugger

3.2 How to know the changes of data inside SFR during simulation?

Click Run – Watch window – select appropriate SFR as required.

3.3 How to simulate program step by step?

In debugging mode, click Run - Step into or simply press F7 key.

3.4 How to send high signal to pin?

Three methods:

Example:

PORTA.0 = 1 ‘ send bit high to pin PORTA0PORTA.B0 = 1 ‘ send bit high to pin PORTA0RA0_bit = 1 ‘ send bit high to pin PORTA0

3.5 How to measure time taken from one location to another location of program?

Click Run - Stopwatch

3.6 How to force signal at certain pin or port to high or low?

Page 12: Mikrobasic Pro

Page 12 of 27

This is applicable if you set the port to input mode in your main program. Click Run – Watch window – Select the required pin – set the signal to high (1) or low (0) as required.

3.7 How to display back program file after clicking start debugger the program file disappears?

Sometimes it might happen your program disappear after clicking start debugger. The following steps show how to redisplay your program file.

Run _ Start Debugger – suddenly all your files disappear. Don’t panic!. Now click Run – Watch Window. Watch window appears. Drag watch window to the right hand side and your program should appear. Wow!

Page 13: Mikrobasic Pro

Page 13 of 27

4.0 Hardware

4.1 What beginners need to know about PIC16F873?

a. Number of PORTs available

PIC16F873 has 3 PORTs: PORTA (5-bit), PORTB (8 bit) and PORTC (8-bit). Only PORTA has analog features.

b. ADCON1

This SFR is used to configure PORTA as DIO or analog.

4.2 How to read very small analog signal e.g. mV?

Change the reference (Vref+) to external voltage reference.

Example:

Adcon1 = 0x81 ‘ You need to apply Vref+ at PORTA3

4.3 What are the components required to build a complete workable PIC system?

Page 14: Mikrobasic Pro

Page 14 of 27

You need :

1. PIC16F873 – 1 unit2. Capacitor 100nF – 2 units3. 10K Resistor – 1 unit4. 4 MHz resonator – 1 unit5. LED – 8 units6. SIP 1K Resistor – 1 unit7. 5V power supply

4.4 How to know the PIC is damage?

Usually during downloading process the PICSTART plus gives error message that unable to program the PIC.

4.5 The PIC fails to work, why?

The PIC is quite robust. You can reprogram many times without fail. However if the PIC fails to work, usually you need to check Configuration bit. If no error was reported during downloading process then mostly likely you enter wrong oscillator type in configuration bit.

4.6 What is the maximum source current from PORTs?

The maximum current is 20mA. Your PIC might damage if you exceed this value!

4.7 How to drive high power load using PIC?

You cannot drive more than 20mA load directly from PIC. Use transistor with relay in case you want to control heavy load.

5.0 PICSTART PLUS

5.1 Why PC fails to communicate with PICStart Plus?

Some of the possibilities are:

Page 15: Mikrobasic Pro

Page 15 of 27

a. Wrong communication port number. To change port number: Programmer – Setting - communication – enter correct port number. Then, try enable programmer. Hopefully it will work!

b. Check the communication cable from PC to PICSTART Plus properly connected and the PICSTART Plus is powered.

c. There are cases where the PICSTART Plus is faulty.

5.2 How to connect PICSTART Plus using USB port?

Almost all new notebooks come without RS232 port. You need to buy USB to RS232 converter and make sure you install its related driver software. After you install the software and connect the cable, you need to locate what serial port number your PC allocates for the USB port.

Start – My computer – Under System Tasks - View system information – Hardware – Device manager – look under Port - Prolific USB-to-serial Comm Port (Com9). So, in this case USB is set at port number 9. Use this number in MPLAB setting.

5.3 How to download program using PICSTARTPLUS?

a. Run MPLAB. Under File – Import – look for *.hex file. The PICSTART PLUS needs *.hex file only.

b. Click Programmer – Select Programmer – PICSTART Plus

Page 16: Mikrobasic Pro

Page 16 of 27

c. Click Enable Programmer - Program

6.0 PROGRAM EXAMPLES

EXAMPLE 1: BLINKING LEDS

' Project name: Blinking LED' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873' Description: LEDs at PORTB blink automatically

program on_off_delay

main: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

while (TRUE) PORTB = 0xFF delay_ms(1000) PORTB =0x00 delay_ms(1000) wendend.

EXAMPLE 2: PEDESTRIAN CROSSING

' Project name: Pedestrian Crossing' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873' Description: To simulate pedestrian crossing

program pedestrian

main: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

portb = 0x00

Page 17: Mikrobasic Pro

Page 17 of 27

while (TRUE) if porta.0 = 1 then delay_ms(5000) portb.0 = 0 ' RED portb.1 = 1 ' YELLOW portb.2 = 0 ' GREEN

delay_ms(2000)

portb.0 = 0 portb.1 = 0 portb.2 = 1 ' GREEN

delay_ms(5000)

portb.0 = 1 ' RED portb.1 = 0 portb.2 = 0 else portb.0 = 1 ' RED portb.1 = 0 ' YELLOW portb.2 = 0 ' GREEN end if wendend.

EXAMPLE 3: TRAFFIC LIGHT

' Project name: Traffic light' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873' Description: To simulate one way traffic light

program traffic_light

main: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output portb = 0x00 while (TRUE) portb.0 = 0 ' RED portb.1 = 0 ' YELLOW portb.2 = 1 ' GREEN

Page 18: Mikrobasic Pro

Page 18 of 27

delay_ms(5000)

portb.0 = 0 portb.1 = 1 ' YELLOW portb.2 = 0 delay_ms(1000) portb.0 = 1 ' RED portb.1 = 0 portb.2 = 0 delay_ms(5000) wendend.

EXAMPLE 4: RUNNING LIGHT

' Project name: Running light' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873

program running_light

main: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

portb = 0x00 while (TRUE)

portb = %00000001 delay_ms(500)

portb = %00000010 delay_ms(500)

portb = %00000100 delay_ms(500)

portb = %00001000 delay_ms(500)

portb = %00010000 delay_ms(500)

Page 19: Mikrobasic Pro

Page 19 of 27

portb = %00100000 delay_ms(500)

portb = %01000000 delay_ms(500)

portb = %10000000 delay_ms(500)

wendend.

EXAMPLE 5: STEPPER MOTOR CONTROLLER

' Project name: Stepper Motor Controller' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873

program stepper

main: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

portb = 0x00 while (TRUE) portb = %00000001 delay_ms(200) portb = %00000010 delay_ms(200) portb = %00000100 delay_ms(200) portb = %00001000 delay_ms(200) wendend.

EXAMPLE 6: SERVO MOTOR CONTROLLER

' Project name: Servo Motor Controller' Programmer: Ahmad Maliki Omar' Date: 14 November 2009

Page 20: Mikrobasic Pro

Page 20 of 27

' PIC Type: PIC16F873

program servo dim y as wordmain: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output portb = 0x00 while (TRUE) for y = 1 to 50 portb.0 = 1 delay_us(800) portb.0 = 0 delay_us(19200) next y for y = 1 to 50 portb.0 = 1 delay_us(2500) portb.0 = 0 delay_us(19500) next y wendend.

EXAMPLE 7: SIGNAL FOR SQUARE WAVE INVERTER

' Project name: servo motor' Programmer: Ahmad Maliki Omar' Date: 14 November 2009' PIC Type: PIC16F873

program square_wave dim y as wordmain: ADCON1 = 0x07 ' Set PORTA as DIO TRISA = 0xFF ' Set PORTA as digital input TRISB = 0x00 ' Set PORTA as output TRISC = 0x00 ' Set PORTC as output

portb = 0x00 while (TRUE) portb = %00000000 delay_us(100) portb = %00000001

Page 21: Mikrobasic Pro

Page 21 of 27

delay_us(9900) portb = %00000000 delay_us(200) portb = %00000010 delay_us(9900) portb = %00000000 delay_us(100) wendend.

EXAMPLE 8: ANALOG TO DIGITAL CONVERTER

' Project name: Analog to digital converter 'programmer: Ahmad Maliki program analog_to_digitaldim adc_rd as word

main: ADCON1 = 0x00 ' Set PORTA as analog mode INTCON = 0X00 ' disable interrupt controller TRISA = 0xff ' Set PORTA as input TRISB = 0x00 ' Set PORTC as output TRISC = 0x00 ' Set PORTD as output

while (TRUE) adc_rd = ADC_Read(0) ' get ADC value from channel 0 of PORTA (pin 2) PORTB = adc_rd ' display adc_rd[7..0] PORTC = Hi(adc_rd) ' display adc_rd[9..8] wendend.

EXAMPLE 9: COMPARE TWO ANALOG SIGNAL

'Project name: Comparing two analog signal'Programmer: Ahmad Maliki Omar'Date: 15 November 2009'PIC type: PIC16F873'Clock speed: 4 MHz

program adc1dim abc_rd, adc_rd as word

main:

Page 22: Mikrobasic Pro

Page 22 of 27

ADCON1 = 0x00 ' Set PORTA as analog mode INTCON = 0X00 ' disable interrupt controller TRISA = 0xff ' Set PORTA as input TRISB = 0x00 ' Set PORTB as output TRISC = 0x00 ' Set PORTC as output

while (TRUE)

adc_rd = ADC_Read(0) ' get ADC value from channel 0 of PORTA0 (pin 2) delay_ms(50)

abc_rd = ADC_Read(1) ' get ADC value from channel 0 of PORTA1 (pin 3)delay_ms(50)

if abc_rd < adc_rd then portb.0 = 1 portb.1 = 0 portb.2 = 0

end if

if abc_rd = adc_rd then portb.0 = 0 portb.1 = 1 portb.2 = 0

end if

if abc_rd > adc_rd then portb.0 = 0 portb.1 = 0 portb.2 = 1

end if wendend.

EXAMPLE 10: READ SMALL ANALOG SIGNAL

' Project name: Analog to digital converter 'programmer: Ahmad Maliki program adc_small_sigdim adc_rd as word

main: ADCON1 = 0x01 ' Set PORTA as analog mode with pin 3 as ref refer page 114 INTCON = 0X00 ' disable interrupt controller

Page 23: Mikrobasic Pro

Page 23 of 27

TRISA = 0xff ' Set PORTA as input

TRISB = 0x00 ' Set PORTC as output TRISC = 0x00 ' Set PORTD as output

while (TRUE) adc_rd = ADC_Read(0) ' get ADC value from channel 0 of PORTA (pin 2) PORTB = adc_rd ' display adc_rd[7..0] PORTC = Hi(adc_rd) ' display adc_rd[9..8] wendend.

EXAMPLE 11: COMPARE ANALOG SIGNAL WITH A CONSTANT

'Project name: ADC compare with a constant'Programmer: Ahmad Maliki Omar'Date: 15 November 2009'PIC type: PIC16F873'Clock speed: 4 MHz'Description: Comparing analog input with a constant 100

program adc1dim adc_rd as word

main: ADCON1 = 0x00 ' Set PORTA as analog mode INTCON = 0X00 ' disable interrupt controller TRISA = 0xff ' Set PORTA as input TRISB = 0x00 ' Set PORTB as output TRISC = 0x00 ' Set PORTC as output

while (TRUE)

adc_rd = ADC_Read(0) ' get ADC value from channel 0 of PORTA (pin 2)

if adc_rd < 100 then portb.0 = 1 portb.1 = 0 portb.2 = 0

end if

if adc_rd = 100 then portb.0 = 0 portb.1 = 1 portb.2 = 0

Page 24: Mikrobasic Pro

Page 24 of 27

end if

if adc_rd > 100 then portb.0 = 0 portb.1 = 0 portb.2 = 1

end if wendend.

EXAMPLE 12: SINGLE CHANNEL MULTIPLE PULSE PWM

'Project name: Multiple Pulse PWM'Programmer: Ahmad Maliki Omar'Date: 15 November 2009'PIC type: PIC16F873'Clock speed: 4 MHz

program PWM_Test

dim current_duty, old_duty as byte

main:

adcon1 = 0x07 ' Set PORTA as DIO TRISA = 255 ' configure PORTA pins as input TRISB = 0 ' designate PORTB pins as output TRISC = 0 ' designate PORTC pins as output

PORTB = 0 ' set PORTB to 0 PORTC = 0 ' set PORTC to 0

PWM1_Init(5000) ' Initialize PWM1 module at 5KHz current_duty = 16 ' initial value for current_duty PWM1_Start() ' start PWM1 PWM1_Set_Duty(current_duty) ' Set current duty for PWM1 while (TRUE) ' endless loop if (RA0_bit <> 0) then ' button on RA0 pressed Delay_ms(10) Inc(current_duty) ' increment current_duty PWM1_Set_Duty(current_duty) end if

if (RA1_bit <> 0) then ' button on RA1 pressed

Page 25: Mikrobasic Pro

Page 25 of 27

Delay_ms(10) Dec(current_duty) ' decrement current_duty PWM1_Set_Duty(current_duty) end if

Delay_ms(5) ' slow down change pace a little wendend.

EXAMPLE 13: TWO CHANNELS MULTIPLE PULSE PWM

'Project name: Two channel Multiple Pulse PWM'Programmer: Ahmad Maliki Omar'Date: 15 November 2009'PIC type: PIC16F873'Clock speed: 4 MHz

program PWM_Test

dim current_duty, current_duty1, old_duty, old_duty1 as byte

main:

adcon1 = 0x07 ' Set PORTA as DIO TRISA = 255 ' configure PORTA pins as input TRISB = 0 ' designate PORTB pins as output TRISC = 0 ' designate PORTC pins as output

PORTB = 0 ' set PORTB to 0 PORTC = 0 ' set PORTC to 0

PWM1_Init(5000) ' Initialize PWM1 module at 5KHz PWM2_Init(5000) ' Initialize PWM2 module at 5KHz

current_duty = 16 ' initial value for current_duty current_duty1 = 16 ' initial value for current_duty1

PWM1_Start() ' start PWM1 PWM2_Start() ' start PWM2 PWM1_Set_Duty(current_duty) ' Set current duty for PWM1 PWM2_Set_Duty(current_duty1) ' Set current duty for PWM2

while (TRUE) ' endless loop if (RA0_bit <> 0) then ' button on RA0 pressed Delay_ms(10) Inc(current_duty) ' increment current_duty

Page 26: Mikrobasic Pro

Page 26 of 27

PWM1_Set_Duty(current_duty) end if

if (RA1_bit <> 0) then ' button on RA1 pressed Delay_ms(10) Dec(current_duty) ' decrement current_duty PWM1_Set_Duty(current_duty) end if

if (RA2_bit <> 0) then ' button on RA2 pressed Delay_ms(10) Inc(current_duty1) ' increment current_duty1 PWM2_Set_Duty(current_duty1) end if

if (RA3_bit <> 0) then ' button on RA3 pressed Delay_ms(40) Dec(current_duty1) ' decrement current_duty1 PWM2_Set_Duty(current_duty1) end if

Delay_ms(5) ' slow down change pace a little wendend.

EXAMPLE 14: PWM WITH VARIABLE DUTY CYLE

' Project name: ADC compare with a constant 'programmer: Ahmad Maliki program adc1dim adc_rd as word dim current_duty, current_duty1, old_duty, old_duty1 as bytemain: ADCON1 = 0x00 ' Set PORTA as analog mode INTCON = 0X00 ' disable interrupt controller TRISA = 0xff ' Set PORTA as input

TRISB = 0x00 ' Set PORTC as output TRISC = 0x00 ' Set PORTD as output pwm1_init(5000) pwm1_start() while (TRUE)

Page 27: Mikrobasic Pro

Page 27 of 27

adc_rd = ADC_Read(0) ' get ADC value from channel 0 of PORTA (pin 2) if adc_rd > 255 then adc_rd = 255 else adc_rd = adc_rd end if delay_ms(10) pwm1_set_duty(adc_rd) delay_ms(10) wendend.

.. GOOD LUCK ..