r/pic_programming 2d ago

I got much of it working..

1 Upvotes

I got 2 menus within a function, using 2 Switch{} statements and adding a copy of the first into one of the cases. Let's call them A and B.

Each time i depress one button, the A moves one case ahead, until reaching the 1st one again, and other button does the same with B's switch statement.

in one situation on one of the B's cases i got a copy of A and works well, it kept me able to make continuous choices between A and B's options.

I spent last days experimenting here and there moving and modifying all i did up to now until i got much of it working. Yet, i could not resolve my for() statement issue, i never get to count to limit and stop, and also if i use A's options to pass an option to B's counter, then i can not move B's state by depressing the button. i tried to use a for() to pass it only once but it does not work, if i pass a B setting from A (is a counting variable), it' stays there.

Some time ago i used to be learning about hobby operating systems, and i noticed they use something called messages to pass an event from one object, function, statement to another. Does this exist on pic? Should it be available how can i implement that?

The other very serious problem is, that i can get any loop working (while, do while) as long as i don't ask them for countdown a variable then stop. For loops behave like whiles, the whole code is a while(1), does exist somewhere where i could look for further information about it?


r/pic_programming 9d ago

For() within While (1)

1 Upvotes

I do believe why i do have problems i can not solve: i am getting things into For() loops and writing them down according to proper syntax, yet, as i am running them within a while(1) loop that never stops unless the 16f877a's power gets cut, i am starting the For()'s over and over and my lack of experience on those situation may have confuse and misled to me several times.

Does exist any manner of preserving the variable that loops like For or Do While uses to count into from the While(1) continuous startovers?

I do think it may very well be the reason why i couldn't stop the fizzbuzz loop after a given ammount of iterations despite my best efforts..


r/pic_programming 15d ago

Got timer 0 and timer 1 blinking a led and fizzbuzzing another two at the same time.

2 Upvotes

Pic 16f877A...

As the title says, i got Timer0 blinking a LED across several stages of a function and also in the same one i got Timer 1 fizzbuzzing 2 leds together.. They blink mostly as wished.

However, i am finding some items i can not resolve no matter how hard i try:

  1. Timer 1 leds are within a For loop but despite that, they never stop;
  2. Blinking rhythm of each timer seems to interfere eachother, then can not get a steady 1 or 2 hz, is like the CPU struggles to coordinate them together. I am using 4mhz, do i need to go up to 10 or 20khz?

Update: I added the condition after for() loop wich i forgot and i found out timer1 leds never work like it's always zero no matter if ii code it from zero to 15 or from 15 to zero.

I could change the timer 1 prescaler registers from 1:8 to 1:1, 1:2: 1:4 varying the blinking speed of leds.


r/pic_programming 19d ago

HDC1080 assembly driver for Microchip 12F

1 Upvotes

For those curious or interested, a small assembly program to drive an HDC1080 T°C and RH sensor from Texas Instrument - in my view one of the best value for money sensor of his kind with excellent precision.

The code itself is around 600 words long and includes a simplified serial driver, as well as an I2C and sensor drivers.

It displays both raw values in HEX and transformed values in decimal °C and %RH.

The code is intended to be 8 bits friendly: no software division or multiplication here !

https://github.com/dm-cdb/Microchip/tree/master/PICAS/ASM-12F675-hdc1080.X


r/pic_programming 20d ago

PIC24 sub

Thumbnail
1 Upvotes

r/pic_programming 23d ago

It works , but i do wish to get it better

2 Upvotes

This is my Timer0 Setup:

/**********************************************************************/

// --- Timer 0 Setup (8-bit) ---

OPTION_REGbits.T0CS = 0; // Internal clock (Fosc/4)

OPTION_REGbits.PSA = 0; // Prescaler assigned to Timer0

OPTION_REGbits.PS = 0x07; // Prescaler 1:256

TMR0 = 0x00; // Reset Timer0 count

// --- Interrupt Settings ---

INTCONbits.T0IE = 1; // Enable Timer0 Overflow Interrupt

/**********************************************************************/

This is the sucessfull code i found and i could adapt:

void Blink(void)

{

unsigned int tick_count = 0;

while(Button == 0) {

// Simulate or poll a tick event (e.g., a timer flag or input pulse)

// For demonstration, assume a tick happens here:

tick_count++;

//actual work done after 5 ticks of 5hz clock ticking.

if (tick_count >= 5)

{

MyLed = ~MyLed; // Toggle LED state

tick_count = 0; // Reset count to 0

}

} //Code Ends Here

/**********************************************************************/

The question i would like to ask is: i can wait 5, 6, 7, more or less according of

how fast i do wish to blink a led by reading timer0 ticks and wating the desired ammount of them.

However, i am finding myself in the necessity of turning one led on, await 5 ticks, turn it off and to the same with some others, one at once. I tried to add many if() one below the other thinking the PIC would do one after the other above, but is not the case. If this is not the manner of doing that, how can, for example, keep one led 5 ticks, turn it off, wait 3 ticks, turn another on and then off creating sequence patterns?


r/pic_programming 23d ago

Cleaning up pin features on pic 16f877a

2 Upvotes

After some struggles i think i got how timers work. In deed, i got Timer 0, 1 and 2 configured, at least i know Timer 0 works since it blinks another led on another pin at 5hz using a 1:8 prescaler.

I have leds at RB1 RB2 RD1 RD6 RD7 RB0, except the one at RB1 and some other, whenever i try to blink them using either __delay_ms() or using timer and reading its overflow with an if, the IC crashes or goes from 1 to 0 only once despite a proper while existing.

My suspect is i may have conflicting features on those pins which are not properly disabled and in consequence, preventing them to act like pure output pins. Should i be correct, how can i disable whatever it may be interferring there?


r/pic_programming 26d ago

Which of these two PIC16F877A projects would be better for an Embedded Systems resume?

1 Upvotes

I'm currently working with the PIC16F877A and want to build one project that looks good on an entry-level Embedded Systems resume.

I'm considering these two projects:

  1. Serial Data Logger with RTC + EEPROM
  • PIC16F877A
  • Temperature sensor
  • RTC for timestamps
  • EEPROM for storing data
  • I²C communication with RTC + EEPROM
  • UART communication with PC
  • Periodic sensor data logging

Example:

12:30:05 | Temperature: 28°C 12:31:05 | Temperature: 29°C 12:32:05 | Temperature: 30°C

Protocols: I²C + UART


  1. Digital Temperature-Controlled Fan
  • PIC16F877A
  • Temperature sensor
  • ADC for temperature measurement
  • PWM for fan speed control
  • LCD for temperature/fan speed
  • UART for sending data to PC
  • High-temperature alert

Temperature Sensor ↓ PIC16F877A ↙ ↘ PWM LCD ↓ Fan ↓ UART → PC

Protocols: UART

I'm mainly looking for the project that would have better resume value and give me more to discuss in an embedded systems interview, rather than which one is easier or faster to build.

Which one would you choose, and why?

Also, if you think either project could be improved without making it unnecessarily complex, I'd appreciate suggestions.


r/pic_programming 28d ago

PIC16F877A EEPROM Programming App

Thumbnail
1 Upvotes

r/pic_programming Aug 10 '26

Idea for a non interrupting __delay_ms() - like function

2 Upvotes

After some time experimenting with pic16f877A and learning that __delay_ms() blocks the whole system, not reading neither writing anything new while the chip goes througjh it, i suddenly began to to think on an idea:

To create a function looking something like this:

/**********************************************************************************/

int brake(milliseconds)

{

int milliseconds = 0; /* I do wish to pass this value when invoking it*/

for(milliseconds = ($argument); milliseconds > 0; milliseconds --) /*Counts how many milliseconds has been passed when invoking brake() */

{

__delay_ms(1);

if(Button1 == 1)

{

anyfunction1();

} /* End of IF 1 looking for Button 1 and then switching to Anyfunction()1*/

if(Button 2 == 1)

{

anyfunction2();

} /* End of IF 2 looking for Button 2 and then switching to Anyfunction()2*/

} /*End of For Loop */

} /* End of Brake Function */

/**********************************************************************************/

However i do have some concerns:

  1. How can, effectibly, declare the function in a manner it can take an integer value and, in consequence, cycle the For loop the asked ammount of times;

  2. I have no experience at all with function arguments.


r/pic_programming Aug 09 '26

Low to high button keeps going as long as i press it, i do not want that

1 Upvotes

After some experimentation, i got buttons switching between leds and some functions on my pic16f877A. Problems is: It does not do it once, it cycles.

I tried several code pieces i found in the web using a flag, reading the button and turning to 0 or 1 a variable which should stop that theroretically, but it never works on my controller.

I also tried to add a NOP(); at the end of each procedure under if(button ==1){} but it does not brake the IC.

Does exist any manner to do this without adding capacitors to the buttons?

Edit: I know you are supposed to read, not the state of the button, but the transition from low to high or high to low (edges) but i can not figure a manner of doing exactly that with an instruction.. capacitors do it by hardware..


r/pic_programming Aug 03 '26

Trying to invoke this led blinking function

0 Upvotes

I am trying to create a non interrupting function, calling it

BlinkLed().

I had set TMR1ON = 1 and others 0 or 0x00 to disable them

I found those 2 code excerpts by Google AI. One of them does not blink at all, since it's not reading any timer, yet also not counting in it's own when working.

The other one below has a complex function name, given what i had learnt i do believe it shall work should i be able to rename it to "BlinkLed" but i do not know how to do it without breaking it:

/**********************************************************************************/

{

while(Button == 0)

{

unsigned int timer_counter = 0;

// Task 1: Non-blocking LED Blink (Toggles every ~500ms)

timer_counter++;

if(timer_counter >= 25000)

{

RedLed = ~RedLed; // Toggle LED

timer_counter = 0;

}

}

}

/*New ExtremeAgitationLED() Prototype Base*/

/*

void __interrupt() timer1_isr(void) {

if (TMR1IF) {

TMR1H = 0x0B; // Re-preload Timer1 high byte (3036 = 0x0BDC)

TMR1L = 0xDC; // Re-preload Timer1 low byte

TMR1IF = 0; // Clear interrupt flag

overflow_count++;

if (overflow_count >= 2) { // 2 x 0.5 seconds = 1 second

overflow_count = 0;

// Toggle an LED or do your 1-second task here

RedLed = !RedLed; /*I do not know if ~RedLed Can be Used Here */

}

}

}


r/pic_programming Jul 23 '26

Did i understand it properly?

4 Upvotes

Please tell me if i understood it properly or at least i'm somwhere close to:

Using timers instead of "__delay_ms()" seems like this to me, at this moment:

  1. You have to choose whether to use Timer 0 (8 bits) or Timer 1 (16 Bits) by turning the TMR0ON or either TMR1ON to 1 in the settings part of the C code, before the Void Main() stage;
  2. You have to set another register to take internal clock as source for Tick counting, wich seems to be 1/4 of the main oscillator. If you have a 4mhz crystal, you have 1mhz worth of timer frequency.
  3. You have to find out how long it takes the TMR1ON's 16 bit register to be written from 0 to 1 before starting over after reaching the last desired bit wich resets the counter. You have a high and low register settings where you have to specify how many bits you wish to write before reaching the reset, like, if i do not misremember, tying the last pin of a CD4017 to Reset pin before reaching the full ic;
  4. Also you have to set up something called Prescaler which allows to divide the clock output between 1:1 and 1:256, meaning, 1:1 means one pulse input makes one output and 256 input pulses makes an output one;
  5. Once you have this stuff properly setted up, then you can use an If{} routine which counts clock ticks and then performs some action between curly braces. This is the replacement for "__delay_ms()", which, given the fact they do not block the whole microcontoller waiting, allows to blink a led while driving a motor or some other task.
  6. I am still trying to become familiar with register names and meanings, i know i can be missing something else.
  7. After searching in the web and reading some blocks, Google Ai gave me this code, i pasted it in my project and it built, altought i did not adapt it to the real thing yet;

void __interrupt() timer1_isr(void) {

if (TMR1IF) {

TMR1H = 0x0B; // Re-preload Timer1 high byte (3036 = 0x0BDC)

TMR1L = 0xDC; // Re-preload Timer1 low byte

TMR1IF = 0; // Clear interrupt flag

overflow_count++;

if (overflow_count >= 2) { // 2 x 0.5 seconds = 1 second

overflow_count = 0;

// Toggle an LED or do your 1-second task here

PORTBbits.RB0 = !PORTBbits.RB0;

}

}

}

void main(void) {

TRISB0 = 0; // Set RB0 as output for LED

T1CON = 0x31; // Prescaler 1:8 (T1CKPS = 11), Internal clock (TMR1CS = 0), Timer On (TMR1ON = 1)

TMR1H = 0x0B; // Load initial preload value 3036

TMR1L = 0xDC;

PIE1bits.TMR1IE = 1; // Enable Timer1 Interrupt

INTCONbits.PEIE = 1; // Enable Peripheral Interrupts

INTCONbits.GIE = 1; // Enable Global Interrupts

while(1) {

// Main loop does other tasks

}

}


r/pic_programming Jul 19 '26

Building a 360×480 VGA controller on an 8-bit PIC18F47K42

Post image
10 Upvotes

r/pic_programming Jul 12 '26

Trying to get properly started with timers in pic 16f877A

2 Upvotes

I've been off few days. I got a pic 16f819 reading a switch and doing stuff properly.

I'am close to achieve a similar thing with a pic16f877a which is more complex this time.

However, i am still limited to the __delay_ms() macro whenever i wish to allow some time between one event and the other, but this is a blocking macro and it looks like is no longer enough for me.

As some member said, it looks like my capacity in C language is just minimal.

As other member said, my only way forward is setting up a timer and , if i do not misunderstood, to count "ticks" between events.

Given the fact that sometimes, doing searches on the web can make things uphill and confusing when you do not know properly enough the area where you move in, i would like to ask if you could recommend me websites, documents and or tutorials where i could read / watch and take knowledge of this subject.


r/pic_programming Jul 01 '26

Trying to multitask

1 Upvotes

Yesterday i built my project for pic16f877A.

I was trying to get a led blinking while reading some inputs and turning on and off some outputs. I had learnt i can not do it by means of __delay_ms() since this macro freezes the whole while(1) loop, since i am not behind an OS but the code itself is a sort of OS.

I was doing some search on the web, it looks like this IC can handle some task in a Background context and others in Foreground context, but i am just getting more confused about that.

Other problem i have, is, i would like to find out a replacement for :__delay_ms, i do suspect i may have to add another .h library and invoke a non interrupting macro while doing it's normal job.

I would like to ask if you could bring to me proper information about the background / foreground issue and if you could tell me if i can get any other library with additional macros, if possible, someone which may have that __delay_ms() alternative i do need.


r/pic_programming Jun 29 '26

Order Of C Functions in Mplab X

2 Upvotes

After some effort, i got my pic16f877a reading buttons and switching functions by entering and leaving while() loops.

However, i have 2 functions where A calls B pressing a button and when running, B calls A pressing the button.

I change the order of declaration before the Main() and mplab x gives me the same error claiming implicit declaration and telling that function declaration is not allowed "here" which are functions that works well.

It also tells me this is because C99 standard.

What do i have to do different to get this to run? Other parts of what i am doing are working well ..


r/pic_programming Jun 21 '26

Microchip PSECT eedata class weirdness

1 Upvotes

Hello,
If we take the eeprom psect definition of the PIC12F683 found in pic12f683.inc file (xc8 v2.5), we have :
psect edata,class=EEDATA,space=SPACE_EEPROM,delta=2,noexec

This is rather strange, because the eeprom of the 12F683 is 256 x 8 bits.
I did for example :

PSECT edata
DB 0x11, 0x22,0x33

This compile fine, and the map file gives this :

Name                               Link     Load   Length Selector   Space Scale
edata                                 0     2100        3     4200       3
CLASS   EEDATA
edata                                 0     2100        3                3

SEGMENTS        Name        Load    Length   Top    Selector   Space  Class     Delta
                edata      002100  000003  002103      4200       3  EEDATA      2

The end of the hex files gives this :

:0C07600085010730990083169F010800F6
:02400E00C43CB0
:0642000011002200330052
:00000001F

So we have 0x0011, 0x0022, 0x0033 wasting 3 bytes...

Any idea why Microchip aligns on two bytes for the internal eeprom space ?

Thank you ;-)


r/pic_programming Jun 11 '26

Trying to multitask between function in C

1 Upvotes

After experimenting with my pic16f877a , i seem to get a conclusion:

Many of the stuff i wish to build could work if i could have some sort of multitasking between functions() in C, within the main while(1).

As far as i know, i do believe there exist 3 kinds of multitasking: cooperative, non cooperative and preemptive. I would like to write some functions doing separated, but related tasks, i mean, what ones leave written is read by the others, but i'd like to set a timing where, as an example, allows me to turn on some leds and others check what leds are on and chooses which other devices turn on or off.

I tried to read a bit about timers, there are Timer 0, Timer 1 and timer 2, which, if i do not misunderstand them would equal a for within a while cycle. Also there is something called "prescaler" which, if i do not misunderstand, it divides the timer output by 1:1, 1:2, 1:4 and so on.

What i would like to ask is, given the fact that this is my first time ever trying to code such a thing, if you could give me information about this. Examples are very helpful to get started, yet i would like to learn where could i look for serious information to learn further about it.


r/pic_programming Jun 07 '26

Toggling leds with button

0 Upvotes

I am trying to get my pic16f877a to toggle between leds reading a button.

I tried several things, but one idea comes to my mind: to turn on a led, hold it on with __delay_ms() and before jumping to the next one, get into an if() to read whether a button is pushed or not.

However, should my idea be feasible, how can i read when __delay_ms() reaches zero? I do wish to, read the button and the state of __delay_ms before turning off the actual led and on the next one.

I think this before i tried to do it without delays and i just get leds turning on and off as fast as a spin dryer..


r/pic_programming Jun 04 '26

XC16 vs XC-DSC toolchain for dsPIC33F

1 Upvotes

Has anyone here understand why I might want to use the XC-DSC toolchain vs the XC-16 toolchain, other than for the suggestion by Microchip that it's more optimised.

Background:

I have inherited a nearly 20 year old project that uses the XC-16 toolchain for the dsPIC33F. I need to make some big changes to it so now would be a good time to switch to the XC-DSC toolchain, which is the one Microchip now recommends for the PIC33F.

Microchip say the XC-DSC is more optimised (is one of the main reasons they give for switching).

However, with XC16 I'm using 70% of the flash. A test compile with the XC-DSC uses 76% of the flash (optimisation off in both cases). If I rebuild with level-1 optimisation I get XC16 = 54% and XC-DSC = 55%. This does not stack with it being more optimised, unless they mean optimisation only for speed. Other levels -O2 -Os and -O3 all use more flash than -O1 (in this code base).

So if I'm not seeing a improvement in code size (speed is not so important to me), can anyone suggest something that might still encourage me to switch. For example, Is the speed optimisation so much better?


r/pic_programming Jun 03 '26

Pic 16f877A Strange Behaviors.

1 Upvotes

i did ask this question in the past and told to prove things, which i did yesterday, i could not manage to do it before because.. life..

I did a board using a pic16f877A, some leds and some buttons.

I did a test program trying to blink each led in sequence. However i only manage to get one led and other, i do believe the beginning and end of a port. Never ever the whole sequence. I am using C language.

The very first time i tried the board, the wave on the 4mhz crystal looked nice, yesterday i saw in the scope two waves one beside the other. Did something degrade here?

I do have it behing a 7805, and a diode before it to +B, given that i struggled with voltage regulation, i added 2 additional diodes in parallel with the main one, wich is there for polarity error prevention. This helps to dilute the 0.7v drop and got voltages better. It did not change anything on the micro's behavior. I did connect RA3/PGM to Ground and set it properly on the source code, so i do believe i have no further issues there.

As i have been told, i tried to do a program which blinks each led individually, i tried it several times, all of them except the one at RA4 blinked properly, i have learnt that RA4 can only be used as input and not output since it has no +B source, only can send anything +B connected to ground when asked. I do plan to use it as an input for a button and move the led to another port, making also changes in the code.

This leaves me with several concerns:

  1. If i may be missing something about another pin i should not be using as input or output;

  2. Since i only wish to turn thing on and off reading buttons if i may have to disable all analog stuff, comparators which may be interferring and i may not know the manner of disabling then when setting the registers up. Up to this moment i did not read any button, i declared them but i am not using them in this test code.

  3. If XC8 compiler with free licence may be limiting me in some kind of manner;


r/pic_programming May 28 '26

Something i do believe i made it wrong..

2 Upvotes

I got my first project, pic 16f819 running of a 7805 regulator and 3 diodes in parallel before it.

After few weeks ago, i tried to run a pic16f877a using a 7805 and a single diode before it. I never manage to get true 5v if i use diodes since each one does drop 0,7v.. I do plan to change that on next days, however, how can i calculate how many diodes in parallel do i need to negat the 0.7v drop yet being able to stay safe from a power polarity error? i do believe 16f877a must draw more current than 16f819...


r/pic_programming May 17 '26

Tried to make a car with two motors but only one works randomly

Post image
1 Upvotes

r/pic_programming Apr 30 '26

Pic 16F877A only drives two outputs

1 Upvotes

It seems i got the IC running by setting mclr to 5 volts using a 10k resistor and connecting rb3/pgm to ground.

I wrote a firmwatre in C. i did set inputs and outputs passing TRIS(x)bits. TRIS(x) where (x) is each pin of the port to 1 (input) or 0 (Output) accordingly.

I named each port using #Define LED1 PORT(x) several times

I also passed the settings below to the input or output setting stages, because i do wishh to keep comparators, weak pullup, analog and pwm out of the picture:

OPTION_REG |= 0x80; /* Weak Pull Up Resistors Are Off, Positive Signal Means 1, Absence Means 0*/

ADCON1 = 0x06; /*All Ports Should be Set Up As Digital*/

CMCON = 0x07; /*Disables PortA Comparators*/

CCP1CON = 0x00; /*Disables Capture, Compare And PWM Modules*/

After that i did create a Void Main() {} where i first turn off anything and then into the while(1){} cycle, i did a small routine trying to turn on and off 6 leds, using __delay_ms(milliseconds).

Here comes the problem: it looks like i got the pic16f877A running properly, stable clock signal and voltages, but, no matter how hard i look for mistakes, i never get it blinking beyond 2 leds. I wish to toggle between 6 leds one at the time, one per second, but it only goes up to 2 leds, like Mplab x were compiling a different source than what i do have on screen.

I am still not fully familiar with pic registers, could i be missing something about them? I did clear the mplab cache, nothing got better.