Hi guys, I have already done some research on this task, which I thought was going to be simple, but its not(at least for not for me)!
I am trying to convert a switch/case statement in C from my previous program (see below):
I found a tip from Microchip - http://microchip.wikidot.com/tip:3
In the text they talk about using a macro, which run at build time and not during operation of the pic.... this leads me to believe that the contents of the W register which I intend to use to take in a variable from TMR0 may become static as it will be polled at build time and not continuously.....
Can anyone assist me with finding a way to implement this in ASM correctly. I do not want to write a nested if, else statement for fear of run time performance and lack of clarity.
Thanks in advance! (I also cross posted this on Microchips forum).
I am trying to convert a switch/case statement in C from my previous program (see below):
Code:
/*
* PIC10LF320
*
* 02/2016
*/
#include <xc.h>
#include <stdint.h>
#include <stdlib.h>
// Configuration Bits
#pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = OFF // Low-Voltage Programming Disabled (Low-voltage programming enabled)
#pragma config LPBOR = OFF // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#define _XTAL_FREQ 32000
void delay(int ms)
{
int i;
for (i = 0; i < ms*8; i++) //32KHz/4 cycles = 8k cycles/sec 8cycles=1ms 1cycle=125us
{
asm ("NOP");
}
}
void STANDARD(void)
{
RA2 = 1;
delay (60);
RA2 = 0;
delay (8);
//__delay_us(100);
}
void SLOW(void)
{
RA2 = 1;
delay (120);
RA2 = 0;
delay (8);
//__delay_us(100);
}
void FAST(void)
{
RA2 = 1;
delay (40);
RA2 = 0;
delay (6);
}
// Main application
void main(void)
{
OSCCON = 0b00000010; // IRCF 32kHz_INTOSC;
ANSELA = 0b00000110; // Clear analog input for RA0 - now digital
TRISA0 = 1; // Set Channel RA0 as input
TRISA2 = 0; // Set Channel RA2 as output
LATA0 = 0; // Set RA0 to off initially
LATA2 = 0; // Set RA2 (LED D1) off
OPTION_REG = 0b00000000; // Clearing WP register
WPUA = 0b00000001; // Enabling WP resistor on A0
TRISA1 = 0; // Set Channel RA1 as output
LATA1 = 0; // Set RA1 off
// update code to terminate RA3 as output - update , rework circuit assign RA3 to RA0's position since RA3 is input only!! then terminate RA0
while (1)
{
unsigned int Random = TMR0; //taking value of TMR0 and assigning to var
Random = Random % 10; //modulo 10 to get last digit
if (RA0 == 0 ) //if input is low randomize between STANDARD, SLOW and FAST
{
switch(Random)
{
case 0: case 1: case 2:
STANDARD();
break;
case 3: case 4: case 5:
SLOW();
break;
case 6: case 7: case 8:
FAST();
break;
case 9:
RA2 = 0;
break;
}
}
}
}
I found a tip from Microchip - http://microchip.wikidot.com/tip:3
Code:
movf SWITCH, w
xorlw CASE1
btfsc STATUS, Z ; If SWITCH = CASE1, jump to LABEL1
goto LABEL1
xorlw CASE2^CASE1
btfsc STATUS, Z ; If SWITCH = CASE2, jump to LABEL2
goto LABEL2
xorlw CASE3^CASE2
btfsc STATUS, Z ; If SWITCH = CASE3, jump to LABEL3
goto LABEL3
In the text they talk about using a macro, which run at build time and not during operation of the pic.... this leads me to believe that the contents of the W register which I intend to use to take in a variable from TMR0 may become static as it will be polled at build time and not continuously.....
Can anyone assist me with finding a way to implement this in ASM correctly. I do not want to write a nested if, else statement for fear of run time performance and lack of clarity.
Thanks in advance! (I also cross posted this on Microchips forum).