home | area personale         schemi | tutorial | robotica | pic micro | recensioni         forum | chat irc         faq | contatti         store | Ordina PCB
username
password
cerca

 
FORUM: Pic Micro
Tutto quanto riguarda questi microprocessori... progetti, suggerimenti, aiuti, discussioni...ecc


PORTA analogica in PIC16F873A
     
Autore Messaggio opzioni
e.ferriani





postato il:
23.11.2017, alle ore 13:23
PORTA analogica in PIC16F873A 

Nel mio plastico avrei bisogno di usare i primi due ingressi della porta A come analogici e gli altri come digitali.

Adesso nel simulatore sto leggendo un trimmer in AN0 ed ho la classica routine che legge il trimmer 0-5V. Vorrei aggiungere il secondo sul pin AN1, ma non ho trovato come leggerlo, o almeno come leggere solo quei due input come analogici:

 
void IngressiAnalogici (void) 

ADCON1 = 0b10001101;           //bit7 -> 0 = Left justified. 6 Least Significant bits of ADRESL are read as \"0\". 
                                //bit7 -> 1 = Right justified. 6 Most Significant bits of ADRESH are read as \"0\". 
                                //bit6,5,4 -> Unimplemented: Read as \'0\' 
                                //bit3,2,1,0 -> 1101 = AN0 e AN1 analogiche 

ADCON0 = 0b00000001;            //bit7,6 -> 00 = FOSC/2;  
                                //bit5,4,3 -> 000 = channel 0, (RA0/AN0); 
                                //bit2 -> 0 = A/D conversion not in progress; 
                                //bit1 -> Unimplemented: Read as \'0\' 
                                //bit0 -> 1 = A/D converter module is operating 

 


Coma faccio a settare il tutto per far sì che venga letto AN0 ed AN1 come analogico ed il resto digitale?



Enea Ferriani
picmicro675




una ogni 10 livelli


postato il:
24.11.2017, alle ore 14:09
Leggendo i fogli dati, come minimo. Cosa dicono in proposito?
A me pare che devi segliere da che canale leggere, prima di fare una lettura nel registro ADCON0, bit 5:3.
A pagina 116
The ADCON1, and TRIS registers control the operation
of the A/D port pins. The port pins that are desired as
analog inputs must have their corresponding TRIS bits
set (input). If the TRIS bit is cleared (output), the digital
output level (V OH or V OL) will be converted.
The A/D operation is independent of the state of the
CHS2:CHS0 bits and the TRIS bits.

Che compilatore stai usando ?



Anno nuovo, forum nuovo.
Mi sa che lascio.
primok




una ogni 100 livelli
una ogni 10 livelli


postato il:
25.11.2017, alle ore 10:32

/*
 * File:   main.c
 * Author: PRIMOK_V
 *
 * Created on 25 novembre 2017, 20.05
 */


// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


#include <xc.h>
#include "Lcd.h"
#include  <stdlib.h>
#include <string.h>

#define OUT_RB0 PORTBbits.RB0
#define _CH  ADCON0bits.CHS

unsigned char p[]="    ";
unsigned   char C_AD[]="CH1_AD=";
unsigned   char C_AD2[]="CH2_AD=";
volatile unsigned int trimmer;
char lung;
unsigned char string[]="";
volatile unsigned char ch=0;
volatile unsigned char rg=1;
void interrupt CON_AD(void){
      
    if( PIR1bits.ADIF){
        if(!ch){
            trimmer =(unsigned int) (ADRESL + (ADRESH<<8));
            _CH=0;
        }else{
             trimmer =(unsigned int) (ADRESL + (ADRESH<<8));
           _CH=1; 
        }
             
        ch^=1;
        rg=ch;
     PIR1bits.ADIF=0;
    }
   
}

void main(void) {
    
  INTCON = 0b00000000;
  TRISA = 0b00001011;   // AN0,AN1,AN3 input
  TRISB = 0b00000000;   // all PORTB bits are output
  TRISC = 0b00000000;
  PORTB = 0b00000000; // clear PORTB
  PORTC = 0b00000000; // clear PORTC 
  ADCON1 =0b10000100;
  ADCON0 = 0b00001001;
  
  PIE1bits.ADIE=1;
  PIR1bits.ADIF=0;
  INTCONbits.GIE=1;
  INTCONbits.PEIE=1;
  
LCD_INIT();   //Inizializza display LCD
__delay_ms(10);
 LCD_CLEAR();
__delay_ms(100);

_CH=0;
lcd_gotoxy(1,1);
lcd_puts(C_AD);
lcd_gotoxy(2,1);
lcd_puts(C_AD2);
ADCON0bits.GO = 1; 
  
 while(1){
    
     
     itoa(string,trimmer, 10);
     lcd_gotoxy(rg,8);
     lcd_puts(string);
     lung=strlen(string); 
     lung+=8;
     lcd_gotoxy(rg,lung); 
     lcd_puts(p); 
     lung=0;
     ADCON0bits.GO = 1; 
     
     
   }
    return;
}
 

 



Più piccola è la mente più grande è la presunzione.
primok




una ogni 100 livelli
una ogni 10 livelli


postato il:
25.11.2017, alle ore 10:37





Più piccola è la mente più grande è la presunzione.
e.ferriani





postato il:
25.11.2017, alle ore 12:59
picmicro675:
Leggendo i fogli dati, come minimo. Cosa dicono in proposito?
A me pare che devi segliere da che canale leggere, prima di fare una lettura nel registro ADCON0, bit 5:3.
A pagina 116
The ADCON1, and TRIS registers control the operation
of the A/D port pins. The port pins that are desired as
analog inputs must have their corresponding TRIS bits
set (input). If the TRIS bit is cleared (output), the digital
output level (V OH or V OL) will be converted.
The A/D operation is independent of the state of the
CHS2:CHS0 bits and the TRIS bits.

Che compilatore stai usando ?

Uso XC8.
Certo che l'ho letto e la porta la setto come ingresso analogico dove serve, ovviamente. Quello che non riesco a fare è la lettura di due pin nello stesso programma.
Ho chiesto dove lavoro e mi hanno detto che i bit della porta vengono letti in modo sequenziale e non tutti in una volta, quindi ogni volta che occorre una lettura va attivato il pin da leggere e convertito. Io credevo bastasse settarlo una sola volta all'inizio e basta. Credo sia per questo che non riuscivo a leggerli.
Provo appena riesco.





Enea Ferriani
e.ferriani





postato il:
25.11.2017, alle ore 16:06
Scusa PrimoK, ma "PIR1bits" non è per i pic18? Io ho un pic16, e comunque stavo provando a fare quanto di seguito. Risultato? I due trimmer li riesco a leggere ma non sono indipendenti ed entrambi vanno da 0 a -64, passando per 32000.
Ci sarà qualche int o char sbagliato di sicuro.
Metto il codice.

Ovviamente essendo un programma di studio ci potrebbero essere delle definizioni di variabili o rutines non utilizzate. Non farci caso.



//-----------------------------------------------
// CONFIGURATION
//-----------------------------------------------

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF// Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

//-----------------------------------------------
// INCLUDE
//-----------------------------------------------

#define _XTAL_FREQ  20000000UL

#include "xc.h"
#include "lcd.h"
#include "stdio.h"
#include "stdlib.h"
//#include "stdint.h"
#include "string.h"

//-----------------------------------------------
//DEFINIZIONI (definitions)
//----------------------------------------------- 

unsigned int trimmer_velocita=0; 
unsigned int trimmer_rampa=0; 


int velocita;
int accelera;
int rallenta;

char lung_velocita;
char lung_rampa;

unsigned char cancellaglizeri[]="    "; 
unsigned char messaggio1[]="trimmer";             //messaggio da scrivere
unsigned char messaggio2[]="rampa";             //messaggio da scrivere
unsigned char string_velocita[]="";
unsigned char string_rampa[]="";

//-----------------------------------------------
//ROUTINE INTERNE (subroutines)
//-----------------------------------------------
 
void Settaggi(void);
void SettaggiAnalogica(void);
void LeggiPortaAN0(void);
void LeggiPortaAN1(void);

void ScriviTrimmerVelocitaSuDisplay (void);
void ScriviTrimmerRampaSuDisplay(void);

//-----------------------------------------------
//MAIN - (MAIN PROGRAM)
//-----------------------------------------------
  
void main(void) 


  Settaggi();
  
  LCD_INIT();                 //Inizializza display LCD 
  __delay_ms(10); 
  LCD_CLEAR(); 
  __delay_ms(100); 
  
  lcd_gotoxy(1,1); 
  lcd_puts(messaggio1);
  lcd_gotoxy(2,1); 
  lcd_puts(messaggio2);
  
  
  while(1)
  {
  ScriviTrimmerVelocitaSuDisplay();
  ScriviTrimmerRampaSuDisplay();
  }



//-----------------------------------------------
//SETTAGGI - (SETTINGS)
//-----------------------------------------------

void Settaggi(void)
{
  // 0= USCITA
  // 1= INGRESSO
  
  INTCON  = 0b00000000; 
  
  TRISA   = 0b00000011;   // PORTAingressi attivati: 0,1,
  TRISB   = 0b00000000;   // PORTB tutti i bit in uscita 
  TRISC   = 0b00000000;   // PORTC tutti i bit in uscita 
  
  PORTA   = 0b00000000;   // clear PORTA 
  PORTB   = 0b00000000;   // clear PORTB 
  PORTC   = 0b00000000;   // clear PORTC  
}

//-----------------------------------------------
//SETTAGGI ANALOGICA
//-----------------------------------------------

void SettaggiAnalogica(void)
{  
  ADCON1 = 0b10001101;      //[7][6,5,4][3,2,1,0]           
                            //bit7 -> 0 = Left justified. 6 Least Significant bits of ADRESL are read as ?0?.
                            //bit7 -> 1 = Right justified. 6 Most Significant bits of ADRESH are read as ?0?.
                            //bit6,5,4 -> Unimplemented: Read as '0'
                            //bit3,2,1,0 -> solo RA0 RA1 analogiche
}

void LeggiPortaAN0 (void)
{

ADCON0 = 0b00000001;        //[7,6][5,4,3][2][1][0]
                            //bit7,6 -> 00 = FOSC/2; 
                            //bit5,4,3 -> 000 = channel 0, (RA0/AN0);
                            //bit2 -> 0 = A/D conversion not in progress;
                            //bit1 -> Unimplemented: Read as '0'
                            //bit0 -> 1 = A/D converter module is operating 
  ADCON0bits.GO = 1; 
  while (ADCON0bits.GO_DONE);
  trimmer_velocita =(unsigned int) (ADRESL + (ADRESH<<8));


 void LeggiPortaAN1 (void)
 { 
  ADCON0 = 0b00001001;            //[7,6][5,4,3][2][1][0]
                                  //bit7,6 -> 00 = FOSC/2; 
                                  //bit5,4,3 -> 001 = channel 0, (RA1/AN1);
                                  //bit2 -> 0 = A/D conversion not in progress;
                                  //bit1 -> Unimplemented: Read as '0'
                                  //bit0 -> 1 = A/D converter module is operating 
  ADCON0bits.GO = 1; 
  while (ADCON0bits.GO_DONE);
  trimmer_rampa =(unsigned int) (ADRESL + (ADRESH<<8));

}

// -----------------------  TEST --------------------------------------

void ScriviTrimmerVelocitaSuDisplay (void)
{
  LeggiPortaAN0();
    
  itoa(string_velocita,trimmer_velocita, 10);           //dice che string 
  lcd_gotoxy (1,10);                              //vai a riga,colonna
  lcd_puts(string_velocita);                   //scrive la stringa "string" sul display
  lung_velocita=strlen(string_velocita);                //lung = calcola la lunghezza di "string"
  lung_velocita+=10;                            //somma 1 alla lunghezza calcolata "lung"
  lcd_gotoxy(1,lung_velocita);                 //vai a riga,colonna determinata da "lung"
  lcd_puts(cancellaglizeri);                        //scrivi "    " (4 spazi) per cancellare gli 0 inutili)
  lung_velocita=0;
}
void ScriviTrimmerRampaSuDisplay (void)
{
  LeggiPortaAN1();
    
  itoa(string_rampa,trimmer_rampa, 10);           //dice che string 
  lcd_gotoxy (2,10);                   //vai a riga,colonna
  lcd_puts(string_rampa);                   //scrive la stringa "string" sul display
  lung_rampa=strlen(string_rampa);                //lung = calcola la lunghezza di "string"
  lung_rampa+=10;                            //somma 1 alla lunghezza calcolata "lung"
  lcd_gotoxy(2,lung_rampa);                 //vai a riga,colonna determinata da "lung"
  lcd_puts(cancellaglizeri);                        //scrivi "    " (4 spazi) per cancellare gli 0 inutili)
  lung_rampa=0;
}




Enea Ferriani
e.ferriani





postato il:
25.11.2017, alle ore 16:13
Nel fare copia/incolla del codice, nel main, dopo Settaggi() ho inavvertitamente cancellato la chiamata a " SettaggiAnalogica();"


Enea Ferriani
primok




una ogni 100 livelli
una ogni 10 livelli


postato il:
25.11.2017, alle ore 16:20

Scusa PrimoK, ma "PIR1bits" non è per i pic18?

NO!!
Se tu avessi letto il data shhet non avresti detto questo!

Pag 25-26 del data sheet pic16f87XA

https://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd…

Avendo usato la routine d'interrupt per scansionare alternativamente i 2 canali AN0 e AN1




Più piccola è la mente più grande è la presunzione.
e.ferriani





postato il:
25.11.2017, alle ore 16:34
Non ti arrabbiare, ho cercato male "PIR1bits" e non "Pir1".


Enea Ferriani
primok




una ogni 100 livelli
una ogni 10 livelli


postato il:
25.11.2017, alle ore 21:32
e.ferriani:
Non ti arrabbiare, ho cercato male "PIR1bits" e non "Pir1".


Non ho capito perche non hai usato il mio metodo ad interrupt più efficiente comunque..

 
//-----------------------------------------------
// CONFIGURATION
//-----------------------------------------------

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF// Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

//-----------------------------------------------
// INCLUDE
//-----------------------------------------------

#define _XTAL_FREQ  20000000UL

#include "xc.h"
#include "Lcd.h"
#include "stdio.h"
#include "stdlib.h"
//#include "stdint.h"
#include "string.h"

//-----------------------------------------------
//DEFINIZIONI (definitions)
//----------------------------------------------- 

unsigned int trimmer_velocita=0; 
unsigned int trimmer_rampa=0; 


int velocita;
int accelera;
int rallenta;
char ch=0;
char lung_velocita;
char lung_rampa;

unsigned char cancellaglizeri[]="    "; 
unsigned char messaggio1[]="trimmer";             //messaggio da scrivere
unsigned char messaggio2[]="rampa";             //messaggio da scrivere
unsigned char string_velocita[]="";
unsigned char string_rampa[]="";

//-----------------------------------------------
//ROUTINE INTERNE (subroutines)
//-----------------------------------------------
 
void Settaggi(void);
void SettaggiAnalogica(void);
void LeggiPortaAN0(void);
void LeggiPortaAN1(void);

void ScriviTrimmerVelocitaSuDisplay (void);
void ScriviTrimmerRampaSuDisplay(void);

//-----------------------------------------------
//MAIN - (MAIN PROGRAM)
//-----------------------------------------------

// -----------------------  TEST --------------------------------------

void ScriviTrimmerVelocitaSuDisplay (void)
{
  
  
  trimmer_velocita =(unsigned int) (ADRESL + (ADRESH<<8));
  
  itoa(string_velocita,trimmer_velocita, 10);           //dice che string 
  lcd_gotoxy (1,10);                              //vai a riga,colonna
  lcd_puts(string_velocita);                   //scrive la stringa "string" sul display
  lung_velocita=strlen(string_velocita);                //lung = calcola la lunghezza di "string"
  lung_velocita+=10;                            //somma 1 alla lunghezza calcolata "lung"
  lcd_gotoxy(1,lung_velocita);                 //vai a riga,colonna determinata da "lung"
  lcd_puts(cancellaglizeri);                        //scrivi "    " (4 spazi) per cancellare gli 0 inutili)
  lung_velocita=0;
  
}
void ScriviTrimmerRampaSuDisplay (void)
{
 
  LeggiPortaAN1(); 
  trimmer_rampa =(unsigned int) (ADRESL + (ADRESH<<8)); 
 
  itoa(string_rampa,trimmer_rampa, 10);           //dice che string 
  lcd_gotoxy (2,10);                   //vai a riga,colonna
  lcd_puts(string_rampa);                   //scrive la stringa "string" sul display
  lung_rampa=strlen(string_rampa);                //lung = calcola la lunghezza di "string"
  lung_rampa+=10;                            //somma 1 alla lunghezza calcolata "lung"
  lcd_gotoxy(2,lung_rampa);                 //vai a riga,colonna determinata da "lung"
  lcd_puts(cancellaglizeri);                        //scrivi "    " (4 spazi) per cancellare gli 0 inutili)
  lung_rampa=0;
 
}

  //-----------------------------------------------
//SETTAGGI - (SETTINGS)
//-----------------------------------------------

void Settaggi(void)
{
  // 0= USCITA
  // 1= INGRESSO
  
  INTCON  = 0b00000000; 
  
  TRISA   = 0b00001011;   // PORTAingressi attivati: 0,1,
  TRISB   = 0b00000000;   // PORTB tutti i bit in uscita 
  TRISC   = 0b00000000;   // PORTC tutti i bit in uscita 
  
  PORTA   = 0b00000000;   // clear PORTA 
  PORTB   = 0b00000000;   // clear PORTB 
  PORTC   = 0b00000000;   // clear PORTC  
   
}

//-----------------------------------------------
//SETTAGGI ANALOGICA
//-----------------------------------------------

void SettaggiAnalogica(void)
{  
 ADCON1 =0b10000100;
  
}

void LeggiPortaAN0 (void)
{

  ADCON0bits.CHS=0;



 void LeggiPortaAN1 (void)
 { 
            
  ADCON0bits.CHS=1;//  
}


void main(void) 

    
  Settaggi();
  SettaggiAnalogica();
  LCD_INIT();                 //Inizializza display LCD 
  __delay_ms(10); 
  LCD_CLEAR(); 
  __delay_ms(100); 
  
  lcd_gotoxy(1,1); 
  lcd_puts(messaggio1);
  lcd_gotoxy(2,1); 
  lcd_puts(messaggio2);
  ADCON0=0x01;
  ch=0;
  LeggiPortaAN0 ();
  ADCON0bits.GO = 1;
  while(ADCON0bits.GO_DONE); 
  while(1)
  {
   
   if(ch==0){  
 
      ScriviTrimmerVelocitaSuDisplay();
    }
   
   if(ch==1){     
      
      ScriviTrimmerRampaSuDisplay();
    }
 
     ch^=1;
     
    if(!ch)
         LeggiPortaAN0(); 
     else
         LeggiPortaAN1(); 
      
     ADCON0bits.GO = 1;
     while(ADCON0bits.GO_DONE);       
  }



 




Più piccola è la mente più grande è la presunzione.
segui questo thread con grixFC, per questa funzione devi aver installato il software grixFC

torna su
     

Come utente anonimo puoi leggere il contenuto di questo forum ma per aprire una discussione
o per partecipare ad una discussione esistente devi essere registrato ed accedere al sito




 







 
 
indietro | homepage | torna su copyright © 2004/2024 GRIX.IT - La community dell'elettronica Amatoriale