View previous topic :: View next topic |
Author |
Message |
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 10:20 am |
|
|
How can I use pins "A0" and "A1" to continue counting "PORTC" and complete the 8 bits? Is that possible? I've never seen anything like it, so I have no idea how to do it. I could use a PIC that already has the 8-bit PORT, but I have several PIC 16F684 in stock.
Code: |
#include <16F684.h>
#fuses INTRC_IO, NOWDT, PROTECT, BROWNOUT, NOMCLR, NOCPD
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(c)
#byte porta = 0x05
#byte portc = 0x07
#byte trisa = 0x85
#byte trisc = 0x87
#bit up = 0x05.5
#bit dw = 0x05.4
unsigned int8 counter;
void counter_binary() {
if (up == 0) {
counter++;
output_c(counter); }
if (dw == 0) {
counter--;
output_c(counter); }
delay_ms(300);
}
void main() {
portc = 0;
trisa = 0b11111100;
trisc = 0b00000000;
while(true) {
counter_binary();
}
}
|
Proteus simulation image link
https://drive.google.com/file/d/1Eg1fh3AxqAhYNdxpWZLG3EullTr5nb45/view?usp=sharing[/url][/code] |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Sun Apr 12, 2020 10:35 am |
|
|
Try much simpler idiomatic CCS code:
Code: |
#include <16F684.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, NOMCLR, NOCPD
#use delay(clock=4000000)
void counter_binary() {
static unsigned int8 count = 0;
if(!input(PIN_A0)){
count++;
}
if(!input(PIN_A1)){
count--;
}
output_c(count);
}
void main(){
while(TRUE){
counter_binary()
delay_ms(300);
}
}
|
That will handle the TRIS for you. You can later on optimize it for speed.
Last edited by jeremiah on Sun Apr 12, 2020 12:56 pm; edited 1 time in total |
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 12:30 pm |
|
|
jeremiah, my code works and yours works too ... But the fact is that my question was not answered, I need help with: How to use 2 PORT to increment 8 bits? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
Re: Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 12:53 pm |
|
|
emazzu630 wrote: | jeremiah, my code works and yours works too ... But the fact is that my question was not answered, I need help with: How to use 2 PORT to increment 8 bits? |
Can you elaborate on what your question means then? I used two port pins (A0 and A1). What do you mean by "2 port to increment"?
Your original question was:
emazzu630 wrote: | How can I use pins "A0" and "A1" to continue counting "PORTC" and complete the 8 bits? |
Which I showed based on how I read your question. So can you elaborate on what you want it to do differently? |
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 1:13 pm |
|
|
Jeremiah i'm sorry, it was a lack of communication ... I will explain better: "PORTC has only 6 exits, I need 8 exits. I want to use PORTA's" A0 "and" A1 "to form the 2 remaining exits. Is it possible on PIC 16F684? See the image of the simulation on Proteus:
https://drive.google.com/file/d/1Eg1fh3AxqAhYNdxpWZLG3EullTr5nb45/view?usp=sharing[/url][/code]
Last edited by emazzu630 on Sun Apr 12, 2020 1:18 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Sun Apr 12, 2020 1:17 pm |
|
|
Just mask off the two bits. So counter & 3 gives the value to feed to
PortA. Then output counter>>2 to port C. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
Re: Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 1:56 pm |
|
|
Ttelmah already covered how to do it with fast_io set on port A. I updated the non fast_io method with two options (MSB vs LSB styles):
Code: | #include <16F684.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, NOMCLR, NOCPD
#use delay(clock=4000000)
void counter_binary() {
static unsigned int8 count = 0;
if(!input(PIN_A5)){
count++;
}
if(!input(PIN_A4)){
count--;
}
// This puts A1/A0 as LSBs
output_c(count >> 2);
output_bit(PIN_A1, bit_test(count,1));
output_bit(PIN_A0, bit_test(count,0));
// OR this puts A1/A0 as MSBs
output_bit(PIN_A1, bit_test(count,7));
output_bit(PIN_A0, bit_test(count,6));
output_c(count); // assumes C7 and C6 do not exist
}
void main(){
while(TRUE){
counter_binary();
delay_ms(300);
}
} |
|
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Binary increment in 2 PORT in the PIC |
Posted: Sun Apr 12, 2020 10:05 pm |
|
|
The "jeremiah" code worked correctly and the "Ttelmah" tip also worked correctly. Thank you for your help! The CCS Forum has been a very useful tool for the CCS programmer. |
|
|
|