Sie sind auf Seite 1von 29

Page 1 of 29

MIKROBASIC PRO FOR PIC16F873

AHMAD MALIKI OMAR Faculty of Electrical Engineering University Technology MARA Shah Alam SELANGOR Nov 2009

Page 2 of 29

Table of Contents
Table of Contents....................................................................................................... 2 1.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?............5 1.4 How to set the correct PIC type and clock frequency?......................................6 1.5 How to set configuration bit?.............................................................................6 1.6 How to compile a program?...............................................................................7 1.7 How to copy file from other project?.................................................................7 2.0 Programming........................................................................................................ 8 2.1 How to call delay?.............................................................................................8 2.2 How to use IF statement?..................................................................................8 2.3 What is the default mode of PORTA of PIC16F873?...........................................9 2.4 How to set all PORTA of PIC16F873 to digital I/O?.............................................9 2.5 What are TRISA, TRISB and TRISC?....................................................................9 2.6 How to define pin with specific name?............................................................10 2.7 How to declare type of variable?.....................................................................10 2.8 How to assign integer literals?........................................................................10 2.9 How to use BUTTON statement?......................................................................11 2.10 How to do increment (INC) or decrement (DEC)?..........................................11 2.11 How to read analog signal?...........................................................................12 3.0 Simulation.......................................................................................................... 13 3.1 How to simulate a program?...........................................................................13 3.2 How to know the changes of data inside SFR during simulation?....................13 3.3 How to simulate program step by step?..........................................................13 3.4 How to send high signal to pin?.......................................................................13 3.5 How to measure time taken from one location to another location of program? .............................................................................................................................. 13

Page 3 of 29 3.6 How to force signal at certain pin or port to high or low?................................13 3.7 How to display back program file after clicking start debugger the program file disappears?........................................................................................................... 14 4.0 Hardware............................................................................................................15 4.1 What beginners need to know about PIC16F873?...........................................15 4.2 How to read very small analog signal e.g. mV?...............................................15 4.3 What are the components required to build a complete workable PIC system? .............................................................................................................................. 15 4.4 How to know the PIC is damage?.....................................................................16 4.5 The PIC fails to work, why?..............................................................................16 4.6 What is the maximum source current from PORTs?........................................16 4.7 How to drive high power load using PIC?.........................................................16 5.0 PICSTART PLUS...................................................................................................17 5.1 Why PC fails to communicate with PICStart Plus?............................................17 5.2 How to connect PICSTART Plus using USB port?..............................................17 5.3 How to download program using PICSTARTPLUS?...........................................18 6.0 PROGRAM EXAMPLES..........................................................................................18 EXAMPLE 1: BLINKING LEDS..................................................................................18 EXAMPLE 2: PEDESTRIAN CROSSING.....................................................................19 EXAMPLE 3: TRAFFIC LIGHT...................................................................................20 EXAMPLE 4: RUNNING LIGHT.................................................................................20 EXAMPLE 5: STEPPER MOTOR CONTROLLER..........................................................21 EXAMPLE 6: SERVO MOTOR CONTROLLER.............................................................22 EXAMPLE 7: SIGNAL FOR SQUARE WAVE INVERTER..............................................23 EXAMPLE 8: ANALOG TO DIGITAL CONVERTER......................................................23 EXAMPLE 9: COMPARE TWO ANALOG SIGNAL.......................................................24 EXAMPLE 10: READ SMALL ANALOG SIGNAL.........................................................25 EXAMPLE 11: COMPARE ANALOG SIGNAL WITH A CONSTANT...............................25 EXAMPLE 12: SINGLE CHANNEL MULTIPLE PULSE PWM.........................................26 EXAMPLE 13: TWO CHANNELS MULTIPLE PULSE PWM...........................................27 EXAMPLE 14: PWM WITH VARIABLE DUTY CYLE....................................................29

Page 4 of 29

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.

Page 5 of 29

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?
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.

Page 6 of 29

1.4 How to set the correct PIC type and clock frequency?
Under View Project setting.

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.

Page 7 of 29

1.6 How to compile a program?


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 dont 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

Page 8 of 29

2.0 Programming
2.1 How to call delay?
Example:

Delay_ms(1000) Delay_us(1000)

Delay 1 second Delay 1ms

2.2 How to use IF statement?


Example:

IF PORTA.0 = 1 THEN PORTB.0 = 1 ELSE PORTB.0 = 0

Page 9 of 29

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 datasheet TRISA = 0xFF TRISB = 0x00 TRISC = 0x00 ' Set PORTA as DIO. Please refer page 114 of ' Set PORTA as digital input ' Set PORTA as output ' 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 output TRISB = 0x0F PORTB0..PORTB3 as input, PORTB4..PORTB7 as output TRISC = 0xFF PORTC is set as input

Page 10 of 29

2.6 How to define pin with specific name?


Example: 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 1

Dim LEDA as sbit at PORTA.0 1-bit size: 0 and 1 Dim as word 16 bit size: 0 .. 65535

Dim tmp as byte 8 bit size: 0 .. 255 Dim tmpa as char8 bit size: 0 .. 255 Dim tmpb as short Dim tmpc as integer 8-bit size: -127 .. 128 16-bit size: -32768 .. 32767

Dim tmpd as longword 32-bit size: 0 .. 4294967295 Dim tmpe as longint 32-bit size: -2147483648 .. 2147483647

Dim tmpf as float 32-bit size: +/- 1.17549435082 x 10-38 .. +/6.80564774407 x 1038 Dim tmpg as byte[30] Array declaration Dim msg as string[string length] const const const const const MAX as longint = 10000 MIN = 1000 ' compiler will assume word type SWITCH = "n" ' compiler will assume char type MSG = "Hello" ' compiler will assume string type 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

Page 11 of 29

$11

hex number

0x00 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) Example: BUTTON(PORTA, 0, 10, 1) connected at the program will recognized as bit high. The push button should be

PORTA0 and after 10ms of signal high

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


Example: Dim dutya, dutyb as word .. .. Main: Dutya = 50 Dutyb = 50 While true Inc(dutya)

Page 12 of 29

Dec(dutyb) Wend End.

2.11 How to read analog signal?


Use Adc_Read(bit) statement to read analog signal. Example: Dim Baca as .. .. Main: ADCON1 = 0x00 Set all PORTA as analog inpout. For detail please refer page 114 of PIC16F873 datasheet INTCON = 0x00 TRISA = 0xFF While true Baca = Adc_read(0) Wend End. Read PORTA0 Disable interrupt Set PORTA as input word

Page 13 of 29

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 PORTA0 PORTA.B0 = 1 send bit high to pin PORTA0 RA0_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 14 of 29

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. Dont panic!. Now click Run Watch Window. Watch window appears. Drag watch window to the right hand side and your program should appear. Wow!

Page 15 of 29

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 16 of 29

You need : 1. PIC16F873 1 unit 2. Capacitor 100nF 2 units 3. 10K Resistor 1 unit 4. 4 MHz resonator 1 unit 5. LED 8 units 6. SIP 1K Resistor 1 unit 7. 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.

Page 17 of 29

5.0 PICSTART PLUS


5.1 Why PC fails to communicate with PICStart Plus?
Some of the possibilities are:
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.

Page 18 of 29

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 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 TRISA = 0xFF TRISB = 0x00 ' Set PORTA as DIO ' Set PORTA as digital input ' Set PORTA as output

Page 19 of 29

TRISC = 0x00 while (TRUE) PORTB = 0xFF delay_ms(1000) PORTB =0x00 delay_ms(1000) wend end.

' Set PORTC as output

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 TRISA = 0xFF TRISB = 0x00 TRISC = 0x00 ' Set PORTA as DIO ' Set PORTA as digital input ' Set PORTA as output ' Set PORTC as output

portb = 0x00 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

Page 20 of 29

portb.0 = 1 ' RED portb.1 = 0 ' YELLOW portb.2 = 0 ' GREEN end if wend end. 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 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) wend end. EXAMPLE 4: RUNNING LIGHT ' ' ' ' Project name: Running light Programmer: Ahmad Maliki Omar Date: 14 November 2009 PIC Type: PIC16F873

Page 21 of 29

program running_light main: ADCON1 = 0x07 TRISA = 0xFF TRISB = 0x00 TRISC = 0x00 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) portb = %00100000 delay_ms(500) portb = %01000000 delay_ms(500) portb = %10000000 delay_ms(500) wend end. EXAMPLE 5: STEPPER MOTOR CONTROLLER ' ' ' ' Project name: Stepper Motor Controller Programmer: Ahmad Maliki Omar Date: 14 November 2009 PIC Type: PIC16F873 ' Set PORTA as DIO ' Set PORTA as digital input ' Set PORTA as output ' Set PORTC as output

Page 22 of 29

program stepper main: ADCON1 = 0x07 TRISA = 0xFF TRISB = 0x00 TRISC = 0x00 ' Set PORTA as DIO ' Set PORTA as digital input ' Set PORTA as output ' 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) wend end. EXAMPLE 6: SERVO MOTOR CONTROLLER ' ' ' ' Project name: Servo Motor Controller Programmer: Ahmad Maliki Omar Date: 14 November 2009 PIC Type: PIC16F873

program servo dim y as word 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) 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

Page 23 of 29

delay_us(2500) portb.0 = 0 delay_us(19500) next y wend end. 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 word 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 = %00000000 delay_us(100) portb = %00000001 delay_us(9900) portb = %00000000 delay_us(200) portb = %00000010 delay_us(9900) portb = %00000000 delay_us(100) wend end. EXAMPLE 8: ANALOG TO DIGITAL CONVERTER ' Project name: Analog to digital converter 'programmer: Ahmad Maliki program analog_to_digital dim adc_rd as word

Page 24 of 29

main: ADCON1 = 0x00 INTCON = 0X00 TRISA = 0xff TRISB = 0x00 TRISC = 0x00

' Set PORTA as analog mode ' disable interrupt controller ' Set PORTA as input ' Set PORTC as output ' 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] wend end. 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 adc1 dim abc_rd, adc_rd as word main: ADCON1 = 0x00 INTCON = 0X00 TRISA = 0xff TRISB = 0x00 TRISC = 0x00 while (TRUE) adc_rd = ADC_Read(0) PORTA0 (pin 2) delay_ms(50) abc_rd = ADC_Read(1) PORTA1 (pin 3) delay_ms(50) if abc_rd < adc_rd then portb.0 = 1 portb.1 = 0 portb.2 = 0 ' get ADC value from channel 0 of ' get ADC value from channel 0 of ' Set PORTA as analog mode ' disable interrupt controller ' Set PORTA as input ' Set PORTB as output ' Set PORTC as output

Page 25 of 29

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 wend end. EXAMPLE 10: READ SMALL ANALOG SIGNAL ' Project name: Analog to digital converter 'programmer: Ahmad Maliki program adc_small_sig dim adc_rd as word main: ADCON1 = 0x01 page 114 INTCON = 0X00 TRISA = 0xff TRISB = 0x00 TRISC = 0x00 ' Set PORTA as analog mode with pin 3 as ref refer ' disable interrupt controller ' Set PORTA as input ' Set PORTC as output ' 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] wend end. EXAMPLE 11: COMPARE ANALOG SIGNAL WITH A CONSTANT 'Project name: ADC compare with a constant

Page 26 of 29

'Programmer: Ahmad Maliki Omar 'Date: 15 November 2009 'PIC type: PIC16F873 'Clock speed: 4 MHz 'Description: Comparing analog input with a constant 100 program adc1 dim adc_rd as word main: ADCON1 = 0x00 INTCON = 0X00 TRISA = 0xff TRISB = 0x00 TRISC = 0x00 while (TRUE) adc_rd = ADC_Read(0) 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 end if if adc_rd > 100 then portb.0 = 0 portb.1 = 0 portb.2 = 1 end if wend end. EXAMPLE 12: SINGLE CHANNEL MULTIPLE PULSE PWM 'Project name: Multiple Pulse PWM 'Programmer: Ahmad Maliki Omar ' get ADC value from channel 0 of ' Set PORTA as analog mode ' disable interrupt controller ' Set PORTA as input ' Set PORTB as output ' Set PORTC as output

Page 27 of 29

'Date: 15 November 2009 'PIC type: PIC16F873 'Clock speed: 4 MHz program PWM_Test dim current_duty, old_duty as byte main: adcon1 = 0x07 TRISA = 255 TRISB = 0 TRISC = 0 PORTB = 0 PORTC = 0 ' Set PORTA as DIO ' configure PORTA pins as input ' designate PORTB pins as output ' designate PORTC pins as output ' set PORTB to 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 Delay_ms(10) Dec(current_duty) ' decrement current_duty PWM1_Set_Duty(current_duty) end if Delay_ms(5) wend end. ' slow down change pace a little

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

Page 28 of 29

'Clock speed: 4 MHz program PWM_Test dim current_duty, current_duty1, old_duty, old_duty1 as byte main: adcon1 = 0x07 TRISA = 255 TRISB = 0 TRISC = 0 PORTB = 0 PORTC = 0 PWM1_Init(5000) PWM2_Init(5000) current_duty = 16 current_duty1 = 16 ' Set PORTA as DIO ' configure PORTA pins as input ' designate PORTB pins as output ' designate PORTC pins as output ' set PORTB to 0 ' set PORTC to 0 ' Initialize PWM1 module at 5KHz ' Initialize PWM2 module at 5KHz ' initial value for current_duty ' 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 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

Page 29 of 29

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) wend end. ' slow down change pace a little

EXAMPLE 14: PWM WITH VARIABLE DUTY CYLE ' Project name: ADC compare with a constant 'programmer: Ahmad Maliki program adc1 dim adc_rd as word dim current_duty, current_duty1, old_duty, old_duty1 as byte main: ADCON1 = 0x00 ' Set PORTA as analog mode INTCON = 0X00 ' disable interrupt controller TRISA = 0xff ' Set PORTA as input TRISB = 0x00 TRISC = 0x00 pwm1_init(5000) pwm1_start() ' Set PORTC as output ' Set PORTD as output

while (TRUE) adc_rd = ADC_Read(0) 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) wend end.

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

.. GOOD LUCK ..

Das könnte Ihnen auch gefallen