Failing to Flash a LED! - Page 1
Forum section: electronics / general

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:01:42 pm
content: Hi all,Please go easy on me, I've been wiring C for about five hours altogether now! Irritatingly I managed this the other day.Code:// PIC18F1330 Configuration Bit Settings// 'C' source line config statements// CONFIG1H#pragma config OSC = INTIO1 // Oscillator (Internal oscillator, CLKO function on RA6, port function on RA7)#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)// CONFIG2L#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOR = BOHW // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)// CONFIG2H#pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)// CONFIG3L#pragma config PWMPIN = OFF // PWM Output Pins Reset State Control bit (PWM outputs disabled upon Reset)#pragma config LPOL = HIGH // Low-Side Transistors Polarity bit (Even PWM Output Polarity Control bit) (PWM0, PWM2 and PWM4 are active-high (default))#pragma config HPOL = HIGH // High Side Transistors Polarity bit (Odd PWM Output Polarity Control bit) (PWM1, PWM3 and PWM5 are active-high (default))// CONFIG3H#pragma config FLTAMX = RA5 // FLTA Mux bit (FLTA input is muxed onto RA5)#pragma config T1OSCMX = LOW // T1OSO/T1CKI MUX bit (T1OSO/T1CKI pin resides on RB2)#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin enabled, RA5 input pin disabled)// CONFIG4L#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Reset on stack overflow/underflow enabled)#pragma config BBSIZ = BB256 // Boot Block Size Select bits (256 Words (512 Bytes) Boot Block size)#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)// CONFIG5L#pragma config CP0 = OFF // Code Protection bit Block 0 (000800-000FFF) (Block 0 is not code-protected)#pragma config CP1 = OFF // Code Protection bit Block 1 (001000-001FFF) (Block 1 is not code-protected)// CONFIG5H#pragma config CPB = OFF // Code Protection bit (Boot Block Memory Area) (Boot Block is not code-protected)#pragma config CPD = OFF // Code Protection bit (Data EEPROM) (Data EEPROM is not code-protected)// CONFIG6L#pragma config WRT0 = OFF // Write Protection bit Block 0 (000800-000FFF) (Block 0 is not write-protected)#pragma config WRT1 = OFF // Write Protection bit Block 1 (001000-001FFF) (Block 1 is not write-protected)// CONFIG6H#pragma config WRTC = OFF // Write Protection bit (Configuration Registers) (Configuration registers are not write-protected)#pragma config WRTB = OFF // Write Protection bit (Boot Block Memory Area) (Boot Block is not write-protected)#pragma config WRTD = OFF // Write Protection bit (Data EEPROM) (Data EEPROM is not write-protected)// CONFIG7L#pragma config EBTR0 = OFF // Table Read Protection bit Block 0 (000800-000FFF) (Block 0 is not protected from table reads executed in other blocks)#pragma config EBTR1 = OFF // Table Read Protection bit Block 1 (001000-001FFF) (Block 1 is not protected from table reads executed in other blocks)// CONFIG7H#pragma config EBTRB = OFF // Table Read Protection bit (Boot Block Memory Area) (Boot Block is not protected from table reads executed in other blocks)// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.#define _XTAL_FREQ 8000000 //set osc. frequency#include <xc.h> //include xc8 header#define PULSE TRISAbits.RA0 //define pin RA0 as PULSE pin#define DIR TRISAbits.RA1 //define pin RA1 as DIR pinint main (){TRISAbits.RA0 = 0; //define A0 as OUTPUTTRISAbits.RA1 = 0; //define A1 as OUTPUTDIR = 1; //Set DIR Pin HIGHwhile (1);{PULSE= ~PULSE;__delay_ms(20); //Toggle Pule H/L Every 20ms}return 0;}All I'm trying to do here is use my PIC to allow for a single speed to a stepper driver.Basically I want Pin RA0 to cycle High/Low every 20ms.I want RA1 to stay permanently High.What on earth am I doing wrong here, it's compiling fine but doing nothing on the chip!Sorry for the stupid questions

author: 22swg
author_rank: Frequent Contributor
timestamp: August 08, 2016, 03:10:56 pm
content: While(1) ( noalso check analogue registers , are the ports digital ?

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:16:24 pm
content: Thanks for the reply 22swg.I imagine (ha!) the output should be digital, I'm just looking for a square-wave from the pin0.Pin1 should be driving high all the time.Have I made a cock-up here with 'while(1)' like I say I've only been doing this for a few hours to please forgive my numptyness!

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:30:35 pm
content: Ah hang on, I think I've spotted one mistake!Let me try again!

author: StillTrying
author_rank: Super Contributor
timestamp: August 08, 2016, 03:31:19 pm
content: #pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled)OFF might be better for now.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:31:45 pm
content: In that PIC RA Port is analog, so you must turn to Digital.Also, you have this;#define PULSE TRISAbits.RA0 //define pin RA0 as PULSE pinthen you have this;{PULSE= ~PULSE;__delay_ms(20); //Toggle Pule H/L Every 20ms}You are changing RA0 from input to output every 20ms.You must do something lik ethis;#define PULSE LATAbits.RA0 //define pin RA0 as PULSE pinRemember. TRIS is for Input/Output, PORT to Read from, and LATA Write to!

author: 22swg
author_rank: Frequent Contributor
timestamp: August 08, 2016, 03:35:37 pm
content: how daft of me thegot corrupted ! should read ( no ; ) on the end of while(1) ... I found the book (C for dummies 6in1 . Dan Gookin ) a great starting place , My C not what you could call professional , more like that of a 4 year old...having fun ...

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:44:55 pm
content: Quote from: 22swg on August 08, 2016, 03:35:37 pmThat one was the error I worked out.Shame about the other glaringly obvious stuffI'm now trying to work out how to configure PortA as digital

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 08, 2016, 03:49:25 pm
content: Quote from: ed_reardon on August 08, 2016, 03:44:55 pmLook at ADCON1 and CMCON register.

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 04:32:48 pm
content: Quote from: ElectricGuy on August 08, 2016, 03:49:25 pmThank you, I've set this both to 0x07 which should swtich both of these registers off.When I try and define the DIR/PULSE at LATAbits.RA0 or RA1 though it breaks the compiler and tells me I've got an 'illegal conversion between types".This is the code as it stands, but won't compile:Code:#define _XTAL_FREQ 8000000 //set osc. frequency#include <xc.h> //include xc8 header#define PULSE LATAbits.RA0 //define pin RA0 as PULSE pin#define DIR LATAbits.RA1int main (){ADCON1 = 0x06; //switch OFF Port-A ADCCMCON = 0x07; //switch OFF comparitorsTRISAbits.RA0 = 0; //define A0 as OUTPUTTRISAbits.RA1 = 0; //define A1 as OUTPUTDIR = 1;while (1){PULSE= ~PULSE;__delay_ms(20); //Toggle Pule H/L Every 20ms}return 0;}I've whipped the header off for clarity!

author: mikerj
author_rank: Super Contributor
timestamp: August 08, 2016, 04:44:11 pm
content: Try the following:Code:#define PULSE LATAbits.LATA0

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 08, 2016, 04:47:39 pm
content: ADCON1 is wrong...

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 04:58:22 pm
content: Quote from: ElectricGuy on August 08, 2016, 04:47:39 pmAh yes, as I've just found out.I'm really sorry for these stupid questions, like I said I only started programming these things this morning in anger!ADCON1 = 0111 any better?

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 08, 2016, 05:10:30 pm
content: better, but check out the imageI can tell you the answer, but is always better for you to search the datasheet.... believe me, you will learn a lot!

author: dannyf
author_rank: Super Contributor
timestamp: August 08, 2016, 05:14:02 pm
content: "I've got an 'illegal conversion between types"."It is a warning from the latest compiler. Regressing to older versions eliminates the warning.

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 05:14:15 pm
content: Quote from: ElectricGuy on August 08, 2016, 05:10:30 pmOh no I agree! I'm here to get stuff wrong and learn, honestly.I'm just very fresh at this!

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 08, 2016, 05:20:02 pm
content: Quote from: ElectricGuy on August 08, 2016, 05:10:30 pmI would understand that as;00000011.Given that I'm typing it is C that would become;ADCON1 = 0b00000011I think? Let's try!

author: Buriedcode
author_rank: Super Contributor
timestamp: August 08, 2016, 05:53:47 pm
content: Whilst it is much easier to read bitmasks as 0b00000000, it can of course be 0x03, or just 3. On that PIC ADCON1 register determines whether pins RA0,1,4 and 6 are analogue or digital, with a '0' for analogue, and a '1' for digital, Its value on power-on-reset is 0x00, which means all those pins are analogue by default. Simply setting this to 0x0F turns them all to digita, but as you're only using RA0, and RA1, then yes 0b00000011 is just fine.It's an annoying quirk of some PICs that some pins default to analogue. But later generations don't have this

author: ed_reardon
author_rank: Regular Contributor
timestamp: August 09, 2016, 06:30:12 am
content: Quote from: Buriedcode on August 08, 2016, 05:53:47 pmWoohoo!! It's working in the simulator at least, the I/O pins are behaving exactly as required!Many thanks all, I'm learning, just slowly.Should the ADCON/CMCON reside in the program main or should they be declared elsewhere? I've got them in the 'main' at the moment but I'm not sure if this is the right way of doing it? Something feels like they should be declared elsewhere?

author: 22swg
author_rank: Frequent Contributor
timestamp: August 09, 2016, 07:55:59 am
content: Hi Ed ... As i tend to build loads of PIC projects 2 or 3 on the go at the same time ! , and come back to old unfinished ones too , i put the IO stuff in a function...like ... so I can figure out what the hell I was doing... I don't use simulator prefer debug..Code:void IO_PORTS_init() // PIC18F45K22 Z80 Programmer.{// pin 2 3 4 5 6 7 - -//PORTA 0 1 2 3 4 5 6 7 (0-6)// use ZrstADCON1bits.PCFG =0x0F; // ALL digital// ALATA=0;TRISA = 0b10111110; // A0 IS Z80 RESETLATAbits.LATA0 = 0;// HOLD Z AT RESET.// pin 33 34 35 36 37 38 39 40 - -//PORTB 0 1 2 3 4 5 6 7 (0-7)// use// BLATB=0;TRISB = 0b11111111; //// pin 15 16 17 18 23 24 25 26//PORTC 0 1 2 3 4 5 6 7 (0-7)// use ZM1 ZRD ZCLK ZWAIT MWR DIS TX RX// C// Z80 CONTROLS & CLOCKLATC=0;TRISC = 0b11000011; //// D Z80 data busLATD=0;TRISD = 0b11111111; ////ELATE=0;TRISE = 0b00000111; // NOT PSP !}

author: Buriedcode
author_rank: Super Contributor
timestamp: August 09, 2016, 10:11:30 am
content: Quote from: ed_reardon on August 09, 2016, 06:30:12 amAlthough some hate it, I use MPLABS configurator for setting up ports and peripherals. The reason its less than ideal is that it buries set up for ports/peripherals in separate files, with the main simply calling functions like swg22 uses, but it does mean you have visual indication of what pins are doing what. For larger projects where I easily lose track of whats happening, I too create my own subroutine for initialization. This leaves the main with just a few calls to set things up before it enters the main loop, and makes it easier to separate out these parts without pushing then far back into includes.For a while I used my own utility to generate templates, with a dirty windows GUI for setting up ports and peripherals, but as I used more PIC's it became an arse to write all the config scripts for it.For the most part I see nothing wrong with having them in the 'main' at the start, but its still annoying having to work out the bits for TRIS, LAT, ANSEL, ADCON etc..

author: dannyf
author_rank: Super Contributor
timestamp: August 09, 2016, 10:57:04 am
content: "Should the ADCON/CMCON reside in the program main or should they be declared elsewhere? "For all of my projects (pic or otherwise), the first thing to be executed in the user space is an initialization routine - mcuinit() in my case. For pics that where I initialize the pins toe gpio, select the internal oscillator frequency, etc.It simplifies matters considerably.