|
|
View previous topic :: View next topic |
Author |
Message |
electronx
Joined: 07 Aug 2021 Posts: 1
|
rotate_right( ),rotate_left() port shifting -confused |
Posted: Fri Aug 13, 2021 12:31 am |
|
|
[img] https://ibb.co/Dk24qxh [/img]
Code: |
#include <16f877a.h>
#fuses XT,NOWDT,NOPUT,NOBROWNOUT,NOLVP,NOCPD,NOWRT,NODEBUG,NOPROTECT
#use delay(clock=4MHz)
void main()
{
int i;
int portb_out;
while(TRUE)
{
portb_out=0b00000001;
for(i=0;i<=7;++i)
{
shift_left(portb_out,??????);
output_b(portb_out);
delay_ms(100);
}
}
}
|
Hi everyone , I want to shift the leds, here I want to do it with rotate_left() command. However, even though I read the ccs manual, I could not get out of it.Any help?
Last edited by electronx on Fri Aug 13, 2021 3:48 am; edited 3 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 13, 2021 2:16 am |
|
|
The output of the program below is as follows:
Code: |
1 2 4 8 16 32 64 128
1 2 4 8 16 32 64 128
1 2 4 8 16 32 64 128
1 2 4 8 16 32 64 128
1 2 4 8 16 32 64 128
.
.
.
|
This was tested in MPLAB vs. 8.92 simulator with CCS vs. 5.104.
Code: | #include <16F877A.h>
#use delay(crystal=4M)
#use rs232(UART1, baud=9600, ERRORS)
//=================================
void main()
{
int8 i;
int8 portb_out;
while(TRUE)
{
portb_out = 1;
for(i=0;i<=7;++i)
{
output_b(portb_out);
printf("%u ", portb_out);
shift_left(&portb_out, 1, 0);
delay_ms(100);
}
printf("\r");
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Aug 13, 2021 6:23 am |
|
|
It is also worth pointing out that you can just use:
portb_out<<=1;
The point about the rotate_left/shift_left functions, is they are designed
to allow you to do rotations on large numbers of bytes in an array.
So, they require the memory address of the array (&), how many bytes
are to be rotated, and a bit to be shifted 'in' to the start.
Hence how PCM shows.
However C has a built in shift, for a variable, using <<.
The syntax I show is equivalent to:
portb_out = portb_out<<1;
So portb_out is loaded with the value from portb_out shifted left one
bit. The version I show, is the 'shortcut' version of this.
In fact the compiler is smart, and realises it is only dealing with one byte,
so in fact the <<= version, and the shift_left version both result in exactly
the same code. So it comes down to 'taste' on whether you want to use
the CCS function, or the standard C way of doing it. |
|
|
|
|
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
|