Ir ao conteúdo
  • Cadastre-se

Programação em c


Posts recomendados

Boa noite galera!

Estou começando na área de programação em C, e em algumas vezes meus projetos não consigo concluir, meu compilador é o MICROC FOR PIC 6.0, e simulo no PROTEUS 7.0.

Bom estou fazendo um carregador de bateria com display LCD e comunicação RS 232, consegui fazer funcionar a parte LCD, porém não consigo enviar o valor de tensão que recebo no pino A0 e A1 do meu PIC 16f877A ao RS232.

 

Código fonte.

 

 char uart_rd;
 
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
 
sbit LCD_RS_direction at trisb2_bit;
sbit LCD_EN_direction at trisb3_bit;
sbit LCD_D4_direction at trisb4_bit;
sbit LCD_D5_direction at trisb5_bit;
sbit LCD_D6_direction at trisb6_bit;
sbit LCD_D7_direction at trisb7_bit;
 
int LeituraAD = 0;
char txt [16];
void main()
{
 ADCON0=0b00000001;
 ADCON1=0b11000100;
 TRISA0_bit= 1;
 TRISA1_bit= 1;
 TRISC= 0;
 TRISD=0;
 PORTC=0;
 PORTD=0;
 
  UART1_INIT (9600);
  Delay_ms(200);                  // Wait for UART 1module to stabilize
 
 
  UART1_Write_TEXT( " conectado " );
  UART1_Write (13);
  UART1_Write (10);
  
   Delay_ms(500);
 
 
 LCD_init();
 LCD_cmd (_LCD_CLEAR);
 LCD_cmd (_LCD_CURSOR_OFF);
 LCD_OUT (1,1,"BATERIA 1");
 LCD_OUT (2,1,"BATERIA 2");
 LCD_OUT (1,15,"V");
 LCD_OUT (2,15,"V");
 
 for(;;)
  {
 unsigned long valorAd = 0, tensaoAD = 0;
 unsigned char txt [6];
 
 valorAD = ADC_Get_Sample (0);
 tensaoAD = (valorAD*1500)/1023;
 WordToStr (tensaoAD,txt);
 txt[0] = (tensaoAD/1000) + 48;
 Lcd_Chr (1,11,txt[0]);
 Lcd_Chr (1,13,'.');
 txt[1] = (tensaoAD/100)% 10 + 48;
 Lcd_Chr (1,12,txt[1]);
  txt[1] = (tensaoAD/10)% 1 + 48;
 Lcd_Chr (1,14,txt[1]);
 
 
 valorAD = ADC_Get_Sample (1);
 tensaoAD = (valorAD*1500)/1023;
 WordToStr (tensaoAD,txt);
 txt[0] = (tensaoAD/1000) + 48;
 Lcd_Chr (2,11,txt[0]);
 Lcd_Chr (2,13,'.');
 txt[1] = (tensaoAD/100)% 10 + 48;
 Lcd_Chr (2,12,txt[1]);
 txt[1] = (tensaoAD/10)% 1 + 48;
 Lcd_Chr (2,14,txt[1]);
 
 
 }
 }
 
Alguém consegue me ajudar?
 
Desde já agradeço.

 

Link para o comentário
Compartilhar em outros sites

Boa tarde!
Acho que faltou a função para enviar o valor pela serial.
Já que está começando em C, segue abaixo 3 exemplos de programação.
Não sei se vai funcionar para com o compilador em questão, mas creio que com alguns pequenos ajustes funcione!

trechos de código para substituir o for(; ; ){...}
 

// Primeira versao float tensaoAD = 0;unsigned char txt[4];    for(; ; )    {    tensaoAD = ((float)ADC_Get_Sample(0)*15.0/1023.0);    sprintf(txt, "%.1f", tensaoAD);    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    UART1_Write_Text("\r\n");        tensaoAD = ((float)ADC_Get_Sample(1)*15.0/1023.0);    sprintf(txt, "%.1f", tensaoAD);    Lcd_Out(2, 11, txt);    UART1_Write_Text(txt);    }// Fim Primeira versao// Segunda versaounsigned int tensaoAD = 0;unsigned char txt[4];    for(; ; )    {    tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(0)*1500/1023);    txt[0] = tensaoAD/1000 + 48;    txt[1] = (tensaoAD/100)%10 + 48;    txt[2] = '.';    txt[3] = (tensaoAD/10)%10 + 48;    txt[4] = '\0';    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);        UART1_Write_Text("\r\n");        tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(1)*1500/1023);    txt[0] = tensaoAD/1000 + 48;    txt[1] = (tensaoAD/100)%10 + 48;    txt[2] = '.';    txt[3] = (tensaoAD/10)%10 + 48;    txt[4] = '\0';    Lcd_Out(2, 11, txt);    UART1_Write_Text(txt);    }// Fim Segunda versao// Terceira versaounsigned int tensaoAD = 0, n, fator;unsigned char txt[4];    while(1)    {    tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(0)*1500/1023);    for(n = 0, fator = 1000; n <= 4; n++)        {        if(n == 2)            txt[n] = '.';        else if(n == 4)            txt[n] = '\0';        else            {            txt[n] = (tensaoAD/fator)%10 + '0';            fator/=10;            }        }    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    UART1_Write_Text("\r\n");        tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(1)*1500/1023);    for(n = 0, fator = 1000; n <= 4; n++)        {        if(n == 2)            txt[n] = '.';        else if(n == 4)            txt[n] = '\0';        else            {            txt[n] = (tensaoAD/fator)%10 + '0';            fator/=10;            }        }    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    }// Fim Segunda versao
Link para o comentário
Compartilhar em outros sites

 

Boa tarde!

Acho que faltou a função para enviar o valor pela serial.

Já que está começando em C, segue abaixo 3 exemplos de programação.

Não sei se vai funcionar para com o compilador em questão, mas creio que com alguns pequenos ajustes funcione!

trechos de código para substituir o for(; ; ){...}

 

// Primeira versao float tensaoAD = 0;unsigned char txt[4];    for(; ; )    {    tensaoAD = ((float)ADC_Get_Sample(0)*15.0/1023.0);    sprintf(txt, "%.1f", tensaoAD);    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    UART1_Write_Text("\r\n");        tensaoAD = ((float)ADC_Get_Sample(1)*15.0/1023.0);    sprintf(txt, "%.1f", tensaoAD);    Lcd_Out(2, 11, txt);    UART1_Write_Text(txt);    }// Fim Primeira versao// Segunda versaounsigned int tensaoAD = 0;unsigned char txt[4];    for(; ; )    {    tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(0)*1500/1023);    txt[0] = tensaoAD/1000 + 48;    txt[1] = (tensaoAD/100)%10 + 48;    txt[2] = '.';    txt[3] = (tensaoAD/10)%10 + 48;    txt[4] = '\0';    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);        UART1_Write_Text("\r\n");        tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(1)*1500/1023);    txt[0] = tensaoAD/1000 + 48;    txt[1] = (tensaoAD/100)%10 + 48;    txt[2] = '.';    txt[3] = (tensaoAD/10)%10 + 48;    txt[4] = '\0';    Lcd_Out(2, 11, txt);    UART1_Write_Text(txt);    }// Fim Segunda versao// Terceira versaounsigned int tensaoAD = 0, n, fator;unsigned char txt[4];    while(1)    {    tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(0)*1500/1023);    for(n = 0, fator = 1000; n <= 4; n++)        {        if(n == 2)            txt[n] = '.';        else if(n == 4)            txt[n] = '\0';        else            {            txt[n] = (tensaoAD/fator)%10 + '0';            fator/=10;            }        }    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    UART1_Write_Text("\r\n");        tensaoAD = (unsigned int)((unsigned long)ADC_Get_Sample(1)*1500/1023);    for(n = 0, fator = 1000; n <= 4; n++)        {        if(n == 2)            txt[n] = '.';        else if(n == 4)            txt[n] = '\0';        else            {            txt[n] = (tensaoAD/fator)%10 + '0';            fator/=10;            }        }    Lcd_Out(1, 11, txt);    UART1_Write_Text(txt);    }// Fim Segunda versao

Acabei de fazer o teste, porme deu uma falha na linha  UART1_INIT (9600); fiz algumas alterações que eu sabia mesmo assim não consegui.

Mais fiquei muito grato pela sua atenção.

OBRIGADO!

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...