|
|
View previous topic :: View next topic |
Author |
Message |
witte
Joined: 07 Feb 2018 Posts: 5
|
Half-Bridge PWM |
Posted: Wed Feb 07, 2018 5:47 pm |
|
|
Hello,
I'm having problems to run a simple code to generate a half-bridge PWM signal.
This is the code:
Code: |
#include <18F4550.h>
#device ADC = 10
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
unsigned int16 i ;
void main(){
setup_oscillator(OSC_8MHZ); // Set internal oscillator to 8MHz
setup_adc(ADC_CLOCK_DIV_8); // Set ADC conversion time to 8Tosc
setup_adc_ports(AN0); // Configure AN0 as analog input
set_adc_channel(0); // Select channel AN0
setup_timer_2(T2_DIV_BY_16, 250, 1); // Set PWM frequency to 500Hz
setup_ccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H);
delay_ms(100); // Wait 100ms
while(TRUE){
i = read_adc(); // Read from AN0 and store in i
set_pwm1_duty(i); // Set pwm1 duty cycle to i
delay_ms(1); // Wait 1ms
}
} |
I am simulating these code into Proteus.
My problem is that I only can see one channel generating the PWM signal.
The other channel stay low level.
My compiler version is Version 4.104.
What am I doing wrong?
Thank you!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 07, 2018 6:51 pm |
|
|
If you were running real hardware, you should see waveforms on the
P1A and P1B pins, which are pins C2 and D5 in the 18F4550. |
|
|
witte
Joined: 07 Feb 2018 Posts: 5
|
|
Posted: Wed Feb 07, 2018 7:02 pm |
|
|
In my case I'm running with Proteus. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Feb 07, 2018 7:27 pm |
|
|
Proteus is busted, broken,corrupted, nonfunctional. As a PIC 'simulator' it does NOT function properly, it is a failure. Please read the sticky called PIC101.
As for 'fixing' Proteus, please ask the Proteus people. Neither Microchip, CCS nor anyone on this forum (I think I speak for most posters here) want to spend any time repairing someone else's code. It would literally take 10s of thousands of hours to get it to 'simulate' a PIC properly. The fact that it's been broken for years and they won't fix it is a clear indication they don't care.
If you're interested in PICs, spend $50 for real hardware and get some quality 'bench time'. Every hour on the bench is worth 100 or more 'proteus' hours.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 07, 2018 9:24 pm |
|
|
Quote: | My compiler version is Version 4.104. |
Apparently your version of the compiler doesn't do things quite right,
but it can be fixed as shown in this thread:
https://www.ccsinfo.com/forum/viewtopic.php?t=46458
Note the call to the output_drive() function. |
|
|
witte
Joined: 07 Feb 2018 Posts: 5
|
|
Posted: Thu Feb 08, 2018 9:15 am |
|
|
temtronic wrote: | Proteus is busted, broken,corrupted, nonfunctional. As a PIC 'simulator' it does NOT function properly, it is a failure. Please read the sticky called PIC101.
As for 'fixing' Proteus, please ask the Proteus people. Neither Microchip, CCS nor anyone on this forum (I think I speak for most posters here) want to spend any time repairing someone else's code. It would literally take 10s of thousands of hours to get it to 'simulate' a PIC properly. The fact that it's been broken for years and they won't fix it is a clear indication they don't care.
If you're interested in PICs, spend $50 for real hardware and get some quality 'bench time'. Every hour on the bench is worth 100 or more 'proteus' hours.
Jay |
Hi temtronic, I'm realizing that problems.
I appreciate your comment and I'll do that, buy a development kit and test on it.
But exist some things we'd like to do quickly test, proof of concept, but unfortunately it never works fine.
Thanks! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Feb 08, 2018 10:06 am |
|
|
Yes, the problem is you 'assume' that Proteus is a 100% working program and as you've found out it isn't.
sadly, there are 1,000s who have assumed it as well.
In over 3 decades of 'playing with PICs' I've yet to find a simulator that does actually simulate a PIC properly.
Currently I use a PICKit 3 as my programmer, white boards for testing and use 2 or 3 PICs. Typically I'll use an 18F46K22 for 90% of the projects. While far more than needed for simple tasks, it has 2 hardware UARTs, 2 HW I2C, lots of memory and pins. There may be a similar 16 series PIC (haven't checked) but by using one PIC, I have a set of functions and drivers that I know work and frankly I don't want to spend time reading a new PICs 500 pages of datasheet ! If you can concentrate your time on ONE PIC you'll be better off. BTW a 'newer' PIC might not be a good choice. Some times they have bugs...maybe the compiler has a bug, so if possible find one that's say 2-3 years old( or older ).
Also if you need USB ( to connect to a PC), consider a TTL<>USB module. At $2 you get an EASY way to interface to a PC. NO drivers, NO codespace,NO pcb.... makes life easy.
Jay |
|
|
witte
Joined: 07 Feb 2018 Posts: 5
|
|
Posted: Thu Feb 08, 2018 10:23 am |
|
|
PCM programmer wrote: | Quote: | My compiler version is Version 4.104. |
Apparently your version of the compiler doesn't do things quite right,
but it can be fixed as shown in this thread:
https://www.ccsinfo.com/forum/viewtopic.php?t=46458
Note the call to the output_drive() function. |
Not works for me, but thanks! |
|
|
witte
Joined: 07 Feb 2018 Posts: 5
|
|
Posted: Thu Feb 08, 2018 10:27 am |
|
|
temtronic wrote: | Yes, the problem is you 'assume' that Proteus is a 100% working program and as you've found out it isn't.
sadly, there are 1,000s who have assumed it as well.
In over 3 decades of 'playing with PICs' I've yet to find a simulator that does actually simulate a PIC properly.
Currently I use a PICKit 3 as my programmer, white boards for testing and use 2 or 3 PICs. Typically I'll use an 18F46K22 for 90% of the projects. While far more than needed for simple tasks, it has 2 hardware UARTs, 2 HW I2C, lots of memory and pins. There may be a similar 16 series PIC (haven't checked) but by using one PIC, I have a set of functions and drivers that I know work and frankly I don't want to spend time reading a new PICs 500 pages of datasheet ! If you can concentrate your time on ONE PIC you'll be better off. BTW a 'newer' PIC might not be a good choice. Some times they have bugs...maybe the compiler has a bug, so if possible find one that's say 2-3 years old( or older ).
Also if you need USB ( to connect to a PC), consider a TTL<>USB module. At $2 you get an EASY way to interface to a PC. NO drivers, NO codespace,NO pcb.... makes life easy.
Jay |
I don't have problem learn another MCUs, and otherwise each project have a specification and target of cost.
For that problem, I'll buy a dev kit and test.
Thanks a lot!! |
|
|
ZEYNEL
Joined: 15 Feb 2018 Posts: 1
|
code is not missing. |
Posted: Thu Feb 15, 2018 4:06 am |
|
|
Hello.
Proteus is a nice program. It does what you say.
However, each program and device must be set correctly.
It can not be left idle.
No required Registers are left unset.
For this, the fuse settings must be made absolutely.
it works like this.
Code: |
#include <18F4550.h>
#device ADC = 10
// Fuses: PLL1,PLL2,PLL3,PLL4,PLL5,PLL6,PLL10,PLL12,CPUDIV1,CPUDIV2
// Fuses: CPUDIV3,CPUDIV4,NOUSBDIV,USBDIV,XT,XTPLL,EC_IO,EC,ECPLL_IO
// Fuses: ECPLL,INTRC_IO,INTRC,INTXT,INTHS,HS,HSPLL,NOFCMEN,FCMEN
// Fuses: NOIESO,IESO,PUT,NOPUT,NOBROWNOUT,BROWNOUT_SW,BROWNOUT_NOSL
// Fuses: BROWNOUT,BORV45,BORV43,BORV27,BORV21,NOVREGEN,VREGEN,NOWDT
// Fuses: WDT,WDT1,WDT2,WDT4,WDT8,WDT16,WDT32,WDT64,WDT128,WDT256
// Fuses: WDT512,WDT1024,WDT2048,WDT4096,WDT8192,WDT16384,WDT32768
// Fuses: CCP2B3,CCP2C1,NOPBADEN,PBADEN,NOLPT1OSC,LPT1OSC,NOMCLR,MCLR
// Fuses: NOSTVREN,STVREN,NOLVP,LVP,ICSP1,ICSP2,NOXINST,XINST,DEBUG
// Fuses: NODEBUG,PROTECT,NOPROTECT,CPB,NOCPB,CPD,NOCPD,WRT,NOWRT,WRTC
// Fuses: NOWRTC,WRTB,NOWRTB,WRTD,NOWRTD,EBTR,NOEBTR,EBTRB,NOEBTRB
#fuses NOBROWNOUT,NOVREGEN,NOWDT,CCP2C1,NOPBADEN,NOLPT1OSC,NOSTVREN
#fuses NOLVP,ICSP1,NOXINST,NODEBUG,NOPROTECT,NOCPB,NOCPD,NOWRT
#fuses MCLR,INTHS
#use delay(clock=8000000)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
unsigned int16 i ;
void main()
{
setup_oscillator(OSC_8MHZ|OSC_STATE_STABLE); // Set internal oscillator to 8MHz
setup_wdt(WDT_OFF);
port_b_pullups(False);
port_d_pullups(False);
output_a(0x00);
set_tris_e(0b00001000); //MCLR-INPUT
set_tris_a(0b00000001);
set_tris_b(0b00000000);
set_tris_c(0b00000000); output_c(0x00);
set_tris_d(0b00000000);
setup_comparator(NC_NC_NC_NC);
setup_psp(PSP_DISABLED);
setup_spi(SPI_DISABLED);
setup_spi2(SPI_DISABLED);
setup_timer_0(T0_OFF);
setup_timer_1(T1_DISABLED);
setup_timer_3(T3_DISABLED);
setup_ccp2(CCP_OFF);
setup_adc_ports(AN0|VSS_VDD); // Configure AN0 as analog input
setup_adc(ADC_CLOCK_DIV_8|ADC_TAD_MUL_0); // Set ADC conversion time to 8Tosc
set_adc_channel(0); // Select channel AN0
setup_timer_2(T2_DIV_BY_16, 250, 1); // Set PWM frequency to 500Hz
setup_ccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H);
delay_ms(100); // Wait 100ms
while(TRUE)
{
i = read_adc(); // Read from AN0 and store in i
set_pwm1_duty(i); // Set pwm1 duty cycle to i
delay_ms(1); // Wait 1ms
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Thu Feb 15, 2018 4:52 am |
|
|
I'm sorry, but you are 'riding for a fall' if you believe Proteus works at all well....
For a simple set of code, properly set up, it can work. However as soon as you move to more complex peripherals (and even worse _combinations_ of peripherals), it shows it's problems.
It many cases it can be made to work, but often you will find that settings you use to get it working, then stop the real chip from working. Problem then is the poster has spent time getting all the settings 'right' for Proteus, and then has to start again to actually get them to work in the chip.
Now there is a tendency here to simply say 'Proteus is busted', and not look at underlying problems, but this is because many people here are just exhausted from trying to get things working, that 'should work', only then to find it is being tried on Proteus, and there is actually a Proteus problem.
Don't kid yourself that Proteus is useful as a PIC simulator for anything at all complex. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Feb 15, 2018 6:26 am |
|
|
The huge problem I have with Proteus is it's ability to allow a PIC program to run, properly, even though according to the schematic, there is NO xtal or caps connected ! While those that 'love' Proteus will say,'well obviously we install them', as an educational tool Proteus fails,since this SIMPLE basic flaw doesn't show a student what is wrong. Most schematics presented here cannot be assembled in the real World and work. The missing xtal and caps is just the classic example. Were any student to present such a schematic to me ,he would lose 30% for those missing parts. Same holds true for not having a power switch in battery operated products.
I understand some of Proteus does work BUT I do not have any confidence is WHAT portions do work and how accurately they simulate. We all know that CCS does it's best to keep up with the new PICs and bugs reported about older ones,usually some oddball register bits or seldom used peripherals so one has to be very very cautious about how Proteus is updated and corrected.
The best way to learn about PIC is 'on the bench'. You can buy a LOT of PICs and parts for less than $50,even PICs on PCB of $2 ! I know money can be tight but considering cellphones are $500 and sneakers are $200, spending $50 on a hobby that can lead to a future job is money well spent. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|