• Welcome to LusoRobótica - Robótica em Português.
 

PIC_VGA

Started by nunosilvarocha, 30 de September de 2014, 11:14

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

nunosilvarocha

Encontrei um protejo com o Attiny que quase já soluciona o meu problema nesta fase inicial.
Agora estou com outro problema.
No MPlab com Hitech, quando executo algo do tipo:

if (a==1)
    RD1=1;
else
     RD2=1;

O que está a acontecer é que liga o bit 1 ou 2 e desliga o resto dos bits do PORTD.
O que está a acontecer? Como altero apenas um bit sem ter que me preocupar com os restantes?

senso

Não tens de usar PORTD.lat ou algo do género?
Avr fanboy

beirao

No hitech não sei que nunca usei, mas no c18 ou xc8 é LATDbits.LATD1 =1.

Assim de repente, pesquisando no google, alguns exemplos têm como tu tens. se ninguém daqui te ajudar, mete o código todo que pode ser de outra coisa
"O único lugar onde o sucesso vem antes do trabalho, é no dicionário" - Albert Einstein

nunosilvarocha

Cá está o codigo.
O que está a acontecer é que na função 'main' quando entramos no ciclo 'if' ao fazer 'RA=0' desliga o 'PORTA' todo.

#include <htc.h>


__CONFIG(1, INTCLKO);           // Configuração do oscilador como interno
//CPUDIV1-CPU sem divisão de frequência
__CONFIG(2, WDTDIS); //WatchDog desactivado
__CONFIG(3, MCLRDIS); //Pino de reset desactivado
__CONFIG(4, DEBUGEN&LVPDIS);    //DEBUGEN-debug activado
//LVPDIS-Low-Voltage programming disabled
//(necessário para debugging)

void USARTInit();
void USARTescreverByte(char);
void USARTescreverString(const char *);
void USARTescreverLine(const char *);
void USARTescreverInt(int ,unsigned char);
unsigned char USARTlerByte();

void on_off();
void resolution();
void intervalo();

char data;

void main(void)
{
    OSCCON = 0b11001111; //Configuração do oscilador
// 0,1 - Define como oscilador interno
// 2 - Define como oscilador interno estável
// 3 - Define como oscilador interno estável
// a funcionar como clock
// 4,5,6 - Define a frequencia do oscilador para 1MHz
                                        // 7 - Abilita o modo IDLE

//Inicializa Porto A como saida de dados
TRISA=0;
    PORTA=0b00000011;
        //Inicializa Porto C como saida de dados
TRISC=0;
    PORTC=0;

        TRISB=0;
    PORTB=0XFF;
       
        unsigned s=1;
//Inicializar a USART
    USARTInit();
        USARTescreverLine("Escolha a resolucao");
        resolution();
        USARTescreverLine("Ligar o sistema");
        on_off();


    while(1)
    {
            data=USARTlerByte();

            if (data=='0')
            {
                RA0=0;
                intervalo ();
                RA0=1;
            }
            if (data=='1')
            {
                RA1=0;
                intervalo ();
                RA1=1;
            }
            if (data=='9')
            {
                on_off();
            }



       }
}

void on_off()
{
    unsigned int a=0;
   
    while(!a)
    {
        USARTescreverLine("ON(1)|OFF(0)");
        data=USARTlerByte();
        if (data=='1')
        {
            RA2=1;
            USARTescreverLine("Sistema_ON");
            a=1;
        }
        else if (data=='0')
        {
            RA2=0;
            USARTescreverLine("Sistema_OFF");
            a=1;
        }
           
    }
}
void resolution()
{
    unsigned int s=0;
    while(!s)
    {
        data=USARTlerByte();
        if (data=='1')
        {
            PORTB=0b00111111;
            s=1;
        }
        if (data=='2')
        {
            PORTB=0b01011111;
            s=1;
        }
        if (data=='3')
        {
            PORTB=0b01101111;
            s=1;
        }
        if (data=='4')
        {
            PORTB=0b01110111;
            s=1;
        }
        if (data=='5')
        {
            PORTB=0b01111011;
            s=1;
        }
        if (data=='6')
        {
            PORTB=0b10111111;
            s=1;
        }
        if (data=='7')
        {
            PORTB=0b11011111;
            s=1;
        }
        if (data=='8')
        {
            PORTB=0b11111111;
            s=1;
        }
    }
    USARTescreverLine("Resolucao escolhida");
}

void intervalo ()
{
    unsigned int i=0, a=0;
    for(i=0; i<500; i++)
    {
        a++;
    }

}

void USARTInit()
{
//Baud Rate = 9600 Bits por Second
//Gerador de taxa de transmissão SPBRG
SPBRG=25;

//TXSTA REG
TXEN=1;
BRGH=1;

//RCSTA
SPEN=1;
CREN=1; //Habilita Receiver (RX)

//BAUDCON
BRG16=1;

}

void USARTescreverByte(char ch)
{
//Espera até receber alguma coisa
while(!TXIF);

//Lê caracter
TXREG=ch;
}

void USARTescreverString(const char *str)
{
while((*str)!='\0')
{
//Espera até receber alguma coisa
while(!TXIF);

//Lê caracter
TXREG=(*str);

//incrementa para ler o proximo
str++;
}
}



void USARTescreverLine(const char *ln)
{
USARTescreverString(ln);
USARTescreverString("\r\n");
}

void USARTescreverInt(int val,unsigned char field_length)
{
if(val<0)
{
USARTescreverByte('-'); //Write '-' sign for negative numbers.
val=(val*(-1)); //Make it positive.
}

//Convert Number To String and pump over Tx Channel.
char str[5]={'0','0','0','0'};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length>5)
while(str[j]==0) j++;
else
j=5-field_length;

for(i=j;i<5;i++)
{
USARTescreverByte('0'+str[i]);
}
}


unsigned char USARTlerByte()
{
while(!RCIF); //Wait for a byte

return RCREG;
}

jmiguelff

O que é o RA??

Tu estas a fazer operações de bits em binário tipo "PORTA=0b00000011;", podes usar a mesma filosofia para trocar um só bit de PORTA.

Sérgio_Sena

Quote from: nunosilvarocha on 21 de October de 2014, 10:01
Cá está o codigo.
O que está a acontecer é que na função 'main' quando entramos no ciclo 'if' ao fazer 'RA=0' desliga o 'PORTA' todo.

#include <htc.h>


__CONFIG(1, INTCLKO);           // Configuração do oscilador como interno
//CPUDIV1-CPU sem divisão de frequência
__CONFIG(2, WDTDIS); //WatchDog desactivado
__CONFIG(3, MCLRDIS); //Pino de reset desactivado
__CONFIG(4, DEBUGEN&LVPDIS);    //DEBUGEN-debug activado
//LVPDIS-Low-Voltage programming disabled
//(necessário para debugging)

void USARTInit();
void USARTescreverByte(char);
void USARTescreverString(const char *);
void USARTescreverLine(const char *);
void USARTescreverInt(int ,unsigned char);
unsigned char USARTlerByte();

void on_off();
void resolution();
void intervalo();

char data;

void main(void)
{
    OSCCON = 0b11001111; //Configuração do oscilador
// 0,1 - Define como oscilador interno
// 2 - Define como oscilador interno estável
// 3 - Define como oscilador interno estável
// a funcionar como clock
// 4,5,6 - Define a frequencia do oscilador para 1MHz
                                        // 7 - Abilita o modo IDLE

//Inicializa Porto A como saida de dados
TRISA=0;
    PORTA=0b00000011;
        //Inicializa Porto C como saida de dados
TRISC=0;
    PORTC=0;

        TRISB=0;
    PORTB=0XFF;
       
        unsigned s=1;
//Inicializar a USART
    USARTInit();
        USARTescreverLine("Escolha a resolucao");
        resolution();
        USARTescreverLine("Ligar o sistema");
        on_off();


    while(1)
    {
            data=USARTlerByte();

            if (data=='0')
            {
                RA0=0;
                intervalo ();
                RA0=1;
            }
            if (data=='1')
            {
                RA1=0;
                intervalo ();
                RA1=1;
            }
            if (data=='9')
            {
                on_off();
            }



       }
}

void on_off()
{
    unsigned int a=0;
   
    while(!a)
    {
        USARTescreverLine("ON(1)|OFF(0)");
        data=USARTlerByte();
        if (data=='1')
        {
            RA2=1;
            USARTescreverLine("Sistema_ON");
            a=1;
        }
        else if (data=='0')
        {
            RA2=0;
            USARTescreverLine("Sistema_OFF");
            a=1;
        }
           
    }
}
void resolution()
{
    unsigned int s=0;
    while(!s)
    {
        data=USARTlerByte();
        if (data=='1')
        {
            PORTB=0b00111111;
            s=1;
        }
        if (data=='2')
        {
            PORTB=0b01011111;
            s=1;
        }
        if (data=='3')
        {
            PORTB=0b01101111;
            s=1;
        }
        if (data=='4')
        {
            PORTB=0b01110111;
            s=1;
        }
        if (data=='5')
        {
            PORTB=0b01111011;
            s=1;
        }
        if (data=='6')
        {
            PORTB=0b10111111;
            s=1;
        }
        if (data=='7')
        {
            PORTB=0b11011111;
            s=1;
        }
        if (data=='8')
        {
            PORTB=0b11111111;
            s=1;
        }
    }
    USARTescreverLine("Resolucao escolhida");
}

void intervalo ()
{
    unsigned int i=0, a=0;
    for(i=0; i<500; i++)
    {
        a++;
    }

}

void USARTInit()
{
//Baud Rate = 9600 Bits por Second
//Gerador de taxa de transmissão SPBRG
SPBRG=25;

//TXSTA REG
TXEN=1;
BRGH=1;

//RCSTA
SPEN=1;
CREN=1; //Habilita Receiver (RX)

//BAUDCON
BRG16=1;

}

void USARTescreverByte(char ch)
{
//Espera até receber alguma coisa
while(!TXIF);

//Lê caracter
TXREG=ch;
}

void USARTescreverString(const char *str)
{
while((*str)!='\0')
{
//Espera até receber alguma coisa
while(!TXIF);

//Lê caracter
TXREG=(*str);

//incrementa para ler o proximo
str++;
}
}



void USARTescreverLine(const char *ln)
{
USARTescreverString(ln);
USARTescreverString("\r\n");
}

void USARTescreverInt(int val,unsigned char field_length)
{
if(val<0)
{
USARTescreverByte('-'); //Write '-' sign for negative numbers.
val=(val*(-1)); //Make it positive.
}

//Convert Number To String and pump over Tx Channel.
char str[5]={'0','0','0','0'};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length>5)
while(str[j]==0) j++;
else
j=5-field_length;

for(i=j;i<5;i++)
{
USARTescreverByte('0'+str[i]);
}
}


unsigned char USARTlerByte()
{
while(!RCIF); //Wait for a byte

return RCREG;
}




Uma melhor modificacao de portas ao nivel do BIT, eh fazer da seguinte forma ::

RA0=0  ->    LATA &= 0b11111110
RA0=1  ->    LATA |= 0b00000001

ou se se quiser insistir em usar RA0=x, entao usar LATAbits.LATA0=x   (confirmar no ficheiro de Inlcude como estao as definicoes...)

Ha outras maneiras de o fazer, claro.

Cumprimentos,
Sérgio Sena

Electronic Gear for Musicians

Instagram

beirao

As vezes a malta usa o PORTAbits.RA0 mas não é nada aconselhável; o registo PORTx é para ler o estado, não para escrever. A questão é que se mexermos no registo PORTx em vez do LATx tipicamente o estado altera, mas nalguns uC dá barraca. Fica a nota!
"O único lugar onde o sucesso vem antes do trabalho, é no dicionário" - Albert Einstein

nunosilvarocha

Problema resolvido.

LATx=1;
LATx=0;


Obrigado a todos.