CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Help to pic and max7221

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rosiley



Joined: 29 May 2014
Posts: 18
Location: Brasil

View user's profile Send private message

Help to pic and max7221
PostPosted: Thu Oct 01, 2015 8:43 am     Reply with quote

Good morning, first of all I want to acknowledge the help I have received in this forum and being of great importance for my learning and my projects, more come. I'm drawing a voltmeter with pic and 7-segment display are multiplexed by max 7221, this project will read four analog channels which will be shown on the display, and 6 display for each channel as follows the code below. Cause the processor to access the display I get more to get the same show the value that's where the things get.
Sorry for my english.

below this code.

Code:


#include <16F877.h>           //Inclui arquivo header do PIC 16f877
#device ADC = 10              //Define 10 bits para o resultado da conversão AD

#use delay (clock=20000000)    /*Definição da frequência do cristal para cálculo
                              ..dos delays*/
#fuses HS, NOWDT, PUT,BROWNOUT, NOLVP  //Configuração dos bits de controle

//Definições para a comunicação serial
#use RS232 (Baud=9600, xmit = PIN_C6, rcv = PIN_C7)

#include <stdlib.h>                                   
#include "MAX7221.h"
                     

//variáveis para armazenamento da conversão

int32 ad0,ADC0;      //AN0
int32 ad1,ADC1;      //AN1
int32 ad2,ADC2;      //AN2
int32 ad4,ADC4;      //AN4

/******************************************************************************
variáveis de controle:
cont = controle de índice para recepção serial;
tempo = informação de tempo em 2 segundos
*******************************************************************************/
int  cont=0, tempo=0;

int1 tmp = 0;     /*variável de controle de tempo em 2 segundos para alterarção
                  das informações no LCD*/
int rec[5];       //Variável de recepção de dados pela serial

#INT_TIMER0       //Diretiva de indetificação da int. do Timer 0

void PiscaLED()
{
   static boolean PiscaLed;   //para piscar o led.
   static int contador;       //para contar o tempo de 1 segundo.
   set_timer0(131-get_timer0());
   contador++;
   if(contador == 125)         //Se contou 125 (1 segundo).
   {
      contador=0;             //zera contagem.
      PiscaLed = !PiscaLed;   //Inverte bit;
      output_bit(pin_C0, PiscaLed); //Liga/desliga LED.
   }
}

void Trata_t0()   //Função para tratamento do Timer 0
{
tempo++;          //Incrementa a variável tempo a cada 25ms
if (tempo >= 80)  //Se tempo >= 80 <=> 2 segundos?
    {
    tmp = 1;      //Variável de controle de tempo 2 seg vai para 1
    tempo = 0;    //Retorna a variável tempo em 0
    }
SET_TIMER0(61);   //Retorna o valor inicial ao Timer 0
}

//Formato do protocolo = AN0:0000:AN1:0000:AN4:0000
void escreve_serial()      //Função de envio do protocolo para a serial
{
printf("AN0:%04lu",ad0);   //Envia dados de conversão A/D1
printf(";");               //Controle do protocolo
printf("AN1:%04lu",ad1);   //Envia dados de conversão A/D2
printf(";");               //Controle do protocolo
printf("AN2:%04lu",ad2);   //Envia dados de conversão A/D3
printf(";");               //Controle do protocolo
printf("AN4:%04lu",ad4);   //Envia dados de conversão A/D4
printf("\r\n");            //Controle do protocolo
}

void main()                   //Função principal
{

//Configura as entradas analógicas AN0 a AN4
SETUP_ADC_PORTS(AN0_AN1_AN2_AN4_VSS_VREF);     
//Utiliza o clock interno para tempo de aquisição
SETUP_ADC(ADC_CLOCK_DIV_32);
//Habilita interrupção global
Enable_Interrupts(GLOBAL);
//Habilita interrupção por recepção de dados na serial
Enable_Interrupts(INT_RDA);
//Habilita interrupção pelo overflow do Timer 0
ENABLE_INTERRUPTS(INT_TIMER0);
/*Configura Timer 0 para incremento pelo ciclo de máquina Prescaler de 1:256 e
modo de operação em 8 bits*/
SETUP_TIMER_0(RTCC_INTERNAL | RTCC_DIV_256 | RTCC_8_BIT);
//Inicia o Timer 0 com o valor 61 - Interrupção a cada 25ms
SET_TIMER0(61);

output_C(0x00);            //Faz toda a porta C = 0

/*Faz a limpeza de toda a variável rec e faz o controle de índices cont em 0*/
for(cont = 0; cont < 5; cont++) rec[cont] = '\0';
cont = 0;

while(true)                            //Loop principal
   { 
   SET_ADC_CHANNEL(0);     //Configura o canal de leitura 0
   delay_us(100);          //Tempo de ajuste do canal (necessário)
   ad0 = READ_ADC();       //Faz a conversão AD e a salva na variável ad0
   
   SET_ADC_CHANNEL(1);     //Configura o canal de leitura 1
   delay_us(100);          //Tempo de ajuste do canal (necessário)
   ad1 = READ_ADC();       //Faz a conversão AD e a salva na variável ad0
   
   SET_ADC_CHANNEL(2);     //Configura o canal de leitura 2
   delay_us(100);          //Tempo de ajuste do canal (necessário)
   ad2 = READ_ADC();       //Faz a conversão AD e a salva na variável ad0
   
   SET_ADC_CHANNEL(4);     //Configura o canal de leitura 4
   delay_us(100);          //Tempo de ajuste do canal (necessário)
   ad4 = READ_ADC();       //Faz a conversão AD e a salva na variável ad0
   
   ADC0 = (ad0 * 500) /1023;
   ADC1 = (ad1 * 500) /1023;
   ADC2 = (ad2 * 500) /1023;
   ADC4 = (ad4 * 500) /1023;
   
  if (ad4>=547)               //Se ad4 >= 555 (aprox. 16 volts)
   {
      output_Low(PIN_C1);     //Desliga indicador de low bat
      output_High(PIN_C2);    //Liga indicador de bat ok
   }
   
   if (ad4<=259)              //Se ad4 <= 307 (aprox. 7,5 volts)
   {
      output_High(PIN_C1);    //Liga indicador de low bat
      delay_ms(1000);
      output_Low(PIN_C1);    //Liga indicador de low bat
      delay_ms(1000);
      output_low(PIN_C2);     //Desliga indicador de bat ok
   }
   
   escreve_serial();          //Envia dados pela serial 
           
   delay_us(100);          //Atraso de 100ms
   }
}   
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Oct 02, 2015 4:02 pm     Reply with quote

And what is your question?
Is there a problem? Is something not working as expected? If yes, what is it you expect to see and what is the different behaviour you see?

I had a quick look at your code and here are some comments:
- You say you have received help on this forum and I'm glad we to hear so, but how is it then possible this is your first post?
- When using the hardware RS232, then always add ERRORS in the #use RS232 line. It is easiest to have the compiler handle the errors for you, this is true for 99% of the programs.
- Function Trata_t0() is never used.
- Please be consistent in how you use upper and lower case. You have variables, constants and function names in mixed variations. Good practice is to have variables in lower case, constants in full upper case and function names from the compiler should be written as noted in the manual. As a hint you can add '#use case' to your program which makes the compiler case sensitive (as required in the C standard).
- Please, as a habit write the comments in your code in English. You never know who will read your program. For example, it would have helped the people in this forum a lot.

Code:
output_High(PIN_C1);    //Liga indicador de low bat
is easier to read and easier to maintain when you define a named constant:
Code:
#define LOW_BAT_INDICATOR    PIN_C1

.....
output_high(LOW_BAT_INDICATOR);        // on
Advantage is that your code is now almost self explaining and when you want to change the electrical connections you only have to modify a single line at the top of your program.


Code:
   delay_us(100);          //Atraso de 100ms
I hate it when comments and code don't match. For this reason only it sometimes is better to write less comments. Lines of code like this are easy enough to understand that the comment is not adding any extra info and now is even wrong.
temtronic



Joined: 01 Jul 2010
Posts: 9198
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Oct 03, 2015 5:12 am     Reply with quote

I think what he wants is a 'driver' for the MAX7221 display chip.


If so this might be a good starting point...
https://www.ccsinfo.com/forum/viewtopic.php?t=53189


Jay
rosiley



Joined: 29 May 2014
Posts: 18
Location: Brasil

View user's profile Send private message

PostPosted: Sat Oct 03, 2015 9:29 am     Reply with quote

Quote:
Is there a problem?

Yes, i need see value adc in display and send to pc.

Quote:
Is something not working as expected?

Yes, the adc value don't see .

Quote:
If yes, what is it you expect to see and what is the different behaviour you see?
I had a quick look at your code and here are some comments:
- You say you have received help on this forum and I'm glad we to hear so, but how is it then possible this is your first post?

Sorry, i had help other forum.

Quote:
- When using the hardware RS232, then always add ERRORS in the #use RS232 line. It is easiest to have the compiler handle the errors for you, this is true for 99% of the programs.

I not understanding this question.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Oct 03, 2015 3:45 pm     Reply with quote

rosiley wrote:
Quote:
- When using the hardware RS232, then always add ERRORS in the #use RS232 line. It is easiest to have the compiler handle the errors for you, this is true for 99% of the programs.

I not understanding this question.

instead of
Code:
#use RS232 (Baud=9600, xmit = PIN_C6, rcv = PIN_C7)
write:
Code:
#use RS232 (Baud=9600, xmit = PIN_C6, rcv = PIN_C7, errors)
This extra parameter causes the compiler to generate extra code for handling UART errors. It makes your life easier and should have been the compiler default setting.

Quote:
Yes, the adc value don't see .
Do you see any text on the pc at all?

Try breaking your problem down into smaller bits. For example, first make a very simple program with just a LED blinking. This proves your program starts up.
Then add code to send data to the PC. Just send a simple 'hello world' text. This proves your serial connection is working.
Then add code to read the ADC for 1 channel. Send this raw value to the PC. This proves you can read the ADC.
Continue in this way by making your program more complete in small steps. When a step fails, you know where to look because you only made a small change.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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