Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F628A Rotate Left Thru Carry & Loop Around

Status
Not open for further replies.
Here is a HEX Code for multi color sets without transistion between them. Eventually I will add transitions between color changes to have a smooth FX.
Just make sure your port assignments are correct when building it. And apply pull up resistors on A4 and A5 as usual.
A4= Red, A6=Green, A7=Blue. Your Anodes are: A3 A2, A1, A0, B7, B6, B5, B4, B3, B2, B1, B0
 

Attachments

  • A12-ColorMorph-3M12-6.asm
    6.1 KB · Views: 172
I understand the schematics, but I didn't understand the operations. Although, it looks like you have plenty of time and don't need to worry about efficiency. I think you could run this from the timer interrupt to free up the main loop for other operations.
 
the hex code doesn't really do anything for me. I'm trying to figure out how many pwm levels you have per LED. For example, do you have 16 levels per R, G, and B LED which would provide 16^3 (4096) different shades or colors per RGB LED?
 
Here is the old code. Ignore the Color timeline when looking at this code. That is for the new system to be implemented.
It is rather simple but repetitive.
 

Attachments

  • A12-ColorRot-3M12-1.asm
    16.5 KB · Views: 158
I understand the schematics, but I didn't understand the operations. Although, it looks like you have plenty of time and don't need to worry about efficiency. I think you could run this from the timer interrupt to free up the main loop for other operations.
No timing issues at all since I set all the loop times and not relying on system PWM. Here is a code I wrote for simple RGB morphing of one channel. Althought I am using 12 RGB on 12 channels. It is technically one channel RGB. The original code shrunk from 30 pages plus to 2 pages.
 

Attachments

  • A12-ColorMorph-3M12-6.asm
    6.4 KB · Views: 166
No, thanks. If you can't explain how it works, I'm certainly not going to be able to tell by looking at your source.
One Frame is 12 sets of colors across 12 channels RGB. Repeat one frame over and over and you will have one stationary color sets across 12 RGB going from red to green to blue and all transitions between them. In the code I used 24 Frames to move the color across. 12 Frames for RGB1 to RGB12 and 12 transition Frames to smooth it out.
The more frames you add, the smoother the color transition will be between one frame to next. Hence colors will travel very smoothly across. That said with normal PWM you have a lot of wasted frames. You are going so fast between frames that at some point anything over 20 frames in transition become redundant and you loose brightness of individual LED. (faded out)
The new method uses no frames but rather 252 steps to cycle thru. You are modulating R, G, B at the same time in differnet intensities. Hence color changes will be silky smooth. Also it allows you to enter any train singnal shape you want (not just triangular).
 
Ignore ColorMorph6 Above. Here is the updated version of the code below. RGB Morphing in 2 pages.


Code:
   list        p=16F628A
    #include <p16F628A.inc>
 
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT & _LVP_OFF
     errorlevel    -302        ; supress banksel warning messages during assembly
     errorlevel    -311         ; supress HIGH operator warning messages during assembly
    cblock    0x20
     copyPORTA                ;Declare variables used in the program
     copyPORTB
     LoopCount
     OutCount
     Trans1
     Trans2
     Color1
     Color2
     Speed
    endc
#define        bank0    bcf        STATUS,RP0
#define        bank1    bsf        STATUS,RP0
RESET_VECTOR    org        0x000
START
     movlw    b'00000111'         ;Set all ports as outputs
     movwf    CMCON               ;Disable comparators
     bank1
     clrf    TRISA
     clrf    TRISB
     bank0
;-----------------------------------------------------------------------------------------------
;         Main Program and Subroutines                          |  
;-----------------------------------------------------------------------------------------------
         movlw    d'10'                 ;Range Between 1 to 10
         movwf    Speed              ;Transition Speed 1=Fastest
         movlw    b'00001111'    ;Set Initial Start Color Black
         movwf    Color1
         movlw    b'11111111'
         movwf    copyPORTB  
;-----------------------------------------------------------------------------------------------
Sequence
         call        MorphColors
         goto    Sequence
;-----------------------------------------------------------------------------------------------
MorphColors
         call        Red
         call        Yellow
         call        Green
         call        Aqua
         call        Blue
         call        Magenta
         return
;More color transitions can be added here before "return"
;-----------------------------------------------------------------------------------------------
Red
         movlw    b'00011111'  
         call        PutColor
         return
;-----------------------------------------------------------------------------------------------
Yellow  
         movlw    b'01011111'  
         call        PutColor
         return
;-----------------------------------------------------------------------------------------------
Green
         movlw    b'01001111'  
         call        PutColor
         return
;-----------------------------------------------------------------------------------------------
Aqua
         movlw    b'11001111'  
         call        PutColor
         return
;-----------------------------------------------------------------------------------------------
Blue
         movlw    b'10001111'  
         call        PutColor
         return
;-----------------------------------------------------------------------------------------------
Magenta
         movlw    b'10011111'  
         call        PutColor  
         return
;-----------------------------------------------------------------------------------------------
White
         movlw    b'11011111'  
         call        PutColor  
         return
;-----------------------------------------------------------------------------------------------
Black
         movlw    b'00001111'  
         call        PutColor  
         return
;-----------------------------------------------------------------------------------------------
PutColor
         movwf    Color2
         call         Morph
         movfw    Color2
         movwf    Color1  
         return
;-----------------------------------------------------------------------------------------------
Morph                          
         movlw    d'254'
         movwf    LoopCount
M1
         movfw    LoopCount
         movwf    Trans1
         sublw    d'255'
         movwf    Trans2
M2
         movfw    Color2
         movwf    copyPORTA
         call        Output
         decfsz    Trans2,F
         goto    M2
M3
         movfw    Color1
         movwf    copyPORTA
         call        Output
         decfsz    Trans1,F                                  
         goto    M3
         decfsz    LoopCount
         goto    M1
         Return
;-----------------------------------------------------------------------------------------------
Output
         movfw    Speed               ;Transition Speed
         movwf    OutCount        ;Placing Delay Will Cause Flicker
Out1
         movfw    copyPORTA
         movwf    PORTA          
         movfw    copyPORTB
         movwf    PORTB
         decfsz    OutCount
         goto    Out1
         return
;-----------------------------------------------------------------------------------------------
   end
 
I can see it sets all 12 bits to 1, then sets two colors, and changes duty from Color1 to Color2. The Output routine doesn't change anything after setting the initial value, practically acts as a delay.

I don't understand what goal the rotation of bits would accomplish. I understand the light will be faded proportional to the amount of 0s in the 12 bits, but exactly the same effect could be accomplished by simply pulsing all LEDs on and off synchroniously, except with better than 1/12 resolution.
 
I can see it sets all 12 bits to 1, then sets two colors, and changes duty from Color1 to Color2. The Output routine doesn't change anything after setting the initial value, practically acts as a delay.
I don't understand what goal the rotation of bits would accomplish. I understand the light will be faded proportional to the amount of 0s in the 12 bits, but exactly the same effect could be accomplished by simply pulsing all LEDs on and off synchroniously, except with better than 1/12 resolution.

In the simple morph (No rotate of colors across 12 channels) you are correct "speed" in the output causes an artificial delay. Since I could not count above 255 in the "Morph", I placed this loop in Output to slow things down. I noticed if Speed goes above 8, it causes flickering. Adding a delay routine in here will also cause a flicker. (Toggle between two colors has to be very fast).

Now Talking about ColorRot (12 Channel RGB with color rotation): The goal is to shrink the code and remove duplicate lines. Notice I have 24 sections in the code each calling 4 pair of lines to create saturation. If you look at one set of lines from one section to the next frame, you see the lines are identical and shifted left. So if I create 24 cariables (12 sets for A &B) one time, then next sections in the next frame will be the same lines rotated one over. So instead of mass of sections and frames, I can create one frame with 3 sections and 24 lines (variables), create a master loop, then rotate all 24 varaibles, and Output the colors, do this 24 times to rotate colors across 12 channels. You will have one master routine that takes in color-set in the begining, and generates frames and sections in one shot. This is method1.

There is a second method that I will not mention as it may cause confusion. So if you have more questions on method1 please let me know.
 
If you have time, can you describe what this code is supposed to do, please? Thank you...

Oops! Never mind. A couple posts appeared before I posted my question. I'll read those...

Code:
Output
        movfw   Speed           ; Transition Speed
        movwf   OutCount        ; Placing Delay Will Cause Flicker
Out1
        movfw   copyPORTA
        movwf   PORTA        
        movfw   copyPORTB
        movwf   PORTB
        decfsz  OutCount
        goto    Out1
        return
 
If you have time, can you describe what this code is supposed to do, please? Thank you...

Oops! Never mind. A couple posts appeared before I posted my question. I'll read those...

Code:
Output
        movfw   Speed           ; Transition Speed
        movwf   OutCount        ; Placing Delay Will Cause Flicker
Out1
        movfw   copyPORTA
        movwf   PORTA       
        movfw   copyPORTB
        movwf   PORTB
        decfsz  OutCount
        goto    Out1
        return
This code will repeat Output funtions let's say 8 times. Meaning I am placing copyPORTB in PORTB and copyPORTA in PORTA 8 times each time I call Output.
This is because calling color1 and color2, 255 times is not enough to slow the morphing. Hence if you do not have "Speed" and not do Output 8 times, the color change is very rapid.
If you place a delay in here instead, then you cause a delay between call of Color1 and call of Color2. Hence Color1 and Color2 do not flash simultaneously and causes a flicker. In a sense you are modulating Color1 and Color2 so rapidly that they become one mix color. I hope this helps.
 
Since you guys are talking about colormorph let me talk about how that works. In the begining (god said let there be light) of the program user gives the program TWO colors. (choose: red, yel, grn, aqu, blu, mag, wht, blk). Program does a routine to modulate two colors as the count decrements. First color1 is called 255 times and color2 is called 1 time. As you progress, color1 is called 128 times and color2 is called 128 times (give & take couple). At the end of the master Morph, color1 is called 1 time and color2 is called 254 times. Hence the color output smoothly goes from color1 to color2. If you choose color1 to be red and color2 to be yellow then you go from red to orange to yellow. Then you give it a new color. Move old color1 into color2 and put new color in color 1. Do morphing again......so on. If I choose the colors in natural order of rainbow then I will have a full transition of all colors. Now the beauty of this program is that I am not limited to red, green, and blue. I can choose from 8 colors including white and black. So I can go from red to yellow to green and then back to yellow and red. Or do any kind of morphing sequence I want. Place white as one of your colors and your color morphing gets washed out. Use black (no color) and your color morphing fades out. Use white or black in between other colors and you can make cool transitions. No need for a fancyPWM, no need for keeping track of times (pre and post scale), and you get 100% duty cycle if you choose to so (you have maximum brightness).
 
In the code you can completely ignore copyPORTB and have your positive coming from source to common anode RGB modules with current limiting resistors of course. I had a reason for my madness to include PortB and feed individual RGB from 12 outputs (B0-B7 and A0-A3). In fact you don''t even need a PIC16F628A and you can do this with an 8 pin PIC like 12F683.
 
I look at the code in post #28, and I see that PORTB and 4 low bits of PORTA are always one, and A4, A6, and A7 are used to gradually replace one color with the other during the course of subroute called "Morph".

However, I don't see any connection to the previously discussed rotation task. Am I missing something?
 
ColorRot: Objective is to place 12 individual colors across 12 RGB modules with smooth transitions in between them. Then move 12 colors to the left until entire rainbow is rotated across 12 RGB.
Hardware: We are multiplexing 12 x 3. Which is 12 Anodes x 3 colors (R,G,B) cathods.
Anodes are directly fed from 12 pins (A3-A0 and B7-B0), and Cathods are sunk by 3 NPN transistors (A4, A6, A7).
Software: 3 major colors are selected (R,G,B as an example). On the scale of 12 RGB these 3 primary colors are separated evenly with 3 steps in between them. So RGB1=Red, RGB5=Green, RGB9=Blue. Zooming in between R and G, your individual colors become Green , (Yel/Grn) , Yel , (Yel/Red) , Red. These depict colors for RGB5-RGB1
Remember your colors are placed in A7, A6, A4. And A5 is your MC so we do not mess with it. That leaves us with 12 free ports to modulate while we modulate RGB.
Main program calls Frame1. This frame has 3 components (Red, Green, Blue) which I call Sections. So each Frame scans Red, then scans Green, then scans Blue.
In each scan we have different intensity for anode ports (A3-A0 and B7-B0). Numbers below indicate intensity. I clarify how to get those numbers briefly.
Looking at color timeline and ignoring scales on right and bottom and focus on where triangles peak and drop, you get different intensities for each 12 pins.
Frame1
Sec1 (scan color Red, A4=1): PortA: 0001-3210 PortB: 0000-1234
Sec2 (scan color Green, A6=1): PortA: 0100-0000 PortB: 1234-3210
Sec3 (scan color Blue, A7=1): PortA: 1000-1234 PortB: 3210-0000
------
Now to produce intensity (0-4):
Sec1:
A: 0001-1110 B: 0000-1111 then call output
A: 0001-1100 B: 0000-0111 then call output
A: 0001-1000 B: 0000-0011 then call output
A: 0001-0000 B: 0000-0001 then call output
Do the same with all the other 2 sections. Call Frame1 and now you have 12 static colors on 12 RGB modules with smooth fade in between them.
To get movement of all colors across 12 channels and to loop around, you shift the above lines of the section to the left A3-B0. You save the 4 MSB of PortA which are your colors, do your rotation and then add those 4-bits back to each line.
The hard way you will have 12 Frames, each Frame calls 3 Sections, and you will have 36 Sections. But if you look at the code for Sections you will find that the Sections Lines Rotate up as you Rotate the bits left. So 24 Sections are actually duplicate of needed 12 sections. So overall you need 12 Frames, 12 Sections, with 4 sets of lines in each Section.
Your entire motion picture which is moving 12 colors across 12 RGB modules from RGB1 to RGB12 takes 12 Frames.
 
Last edited:
I look at the code in post #28, and I see that PORTB and 4 low bits of PORTA are always one, and A4, A6, and A7 are used to gradually replace one color with the other during the course of subroute called "Morph".

However, I don't see any connection to the previously discussed rotation task. Am I missing something?
North that was the intro to walk you thru ColorRot and how it is implemented. Read my next post which explains ColorRot and its implementation and you see why I needed the Rotations.
 
Here is the color chart with Port labels and color intensity.
 

Attachments

  • 12 CHANNEL RGB COLOR MORPH 12 PORTS.jpg
    12 CHANNEL RGB COLOR MORPH 12 PORTS.jpg
    59.9 KB · Views: 172
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top