Moderadores: andre_luis, 51, guest2003, Renie
int le_analogica()
{
output_high(out_via_1);
output_high(out_via_2);
delay_ms(100);
set_adc_channel(0);
delay_ms(200);
vin1_1 = read_adc();
lcd_pos_xy(1, 1);
printf (lcd_escreve,"Tensao %2.3f mV ",vin1_1);
set_adc_channel(1);
corrente1= read_adc();
lcd_pos_xy(1, 2);
printf (lcd_escreve,"Corrente %4.2f mA ",corrente1);
delay_ms(2000);
output_low(out_via_1);
output_low(out_via_2);
return(vin1_1);
return(corrente1);
//talvez fique menor do que printf()
//display 4 bits ligado em PORTC 7...4
#define lin1 0x80 //linha 1 do display
#define lin2 0xc0 //2
#define LCD_RS RC1
#define LCD_EN RC0
#define LCD_STROBE LCD_EN=1;asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");asm("nop");LCD_EN=0
void lcd_write(unsigned char c)
{
PORTC = (PORTC & 0x0f) | (c & 0xf0);
LCD_STROBE;
c <<= 4;
PORTC = (PORTC & 0x0f) | (c & 0xf0);
LCD_STROBE;
delay(500);
}
/**********************************************************************/
void msgdsp(unsigned char l,unsigned char c,const char * s)
{
LCD_RS=0;
lcd_write(l+c); //posiciona cursor
LCD_RS=1;
while(*s)
lcd_write(*s++);
}
/**********************************************************************/
void lcd_dec(unsigned char l,unsigned char c,unsigned char nd,unsigned int dado)
{
unsigned int mil,cen,dez,un;
mil=dado/1000;
cen=(dado-mil*1000)/100; //pode usar %
dez=(dado-mil*1000-cen*100)/10;
un=dado-mil*1000-cen*100-dez*10;
LCD_RS=0;
lcd_write(l+c);//posiciona cursor
LCD_RS=1;
switch(nd) {
case 4: lcd_write(mil+0x30);//sem break
case 3: lcd_write(cen+0x30);
case 2: lcd_write(dez+0x30);
case 1: lcd_write(un+0x30);
}
}
/**********************************************************************/
void lcd_init(void)
{
LCD_EN=0;
LCD_RS=0; // write control bytes
delay(30); // power on delay
PORTC = 0b00110000; // attention!
LCD_STROBE;
delay(2000); // power on delay
LCD_STROBE;
delay(10000); // power on delay
LCD_STROBE;
delay(2000); // power on delay
PORTC = 0b00100000; // set 4 bit mode
LCD_STROBE;
delay(2000); // power on delay
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x0e); // display off
lcd_write(0x06); // entry mode
lcd_write(0x0c); // entry mode
lcd_write(1); // limpa display
}
void main(void)
{
....
....
....
msgdsp(lin1,0,"oi!");
msgdsp(lin2,6,"tchau!");
lcd_dec(lin1,7,4,1234);
}
WHILE(true)
{
output_high(out_via_1);
output_high(out_via_2);
output_high(out_via_3);
output_high(out_via_4);
set_adc_channel(0);
vin1_1 = read_adc();
vin1_1 = vin1_1*20;
lcd_pos_xy(1,1);
printf (lcd_escreve,"Va %2.2f ",vin1_1);
set_adc_channel(1);
vin2_1 = read_adc();
vin2_1 = vin2_1*20;
lcd_pos_xy(1,2);
printf (lcd_escreve,"Vb %2.2f ",vin2_1);
lcd_pos_xy(11,1);
set_adc_channel(2);
corrente1 = read_adc();
printf (lcd_escreve," Ca %2.2f ",Corrente1);
lcd_pos_xy(11,2);
set_adc_channel(3);
corrente2 = read_adc();
printf (lcd_escreve," Cb %2.2f ",Corrente2);
delay_ms(2000);
output_low(out_via_1);
output_low(out_via_2);
output_low(out_via_3);
output_low(out_via_4);
delay_ms(500);
}
#include <pic.h>
/* Basic A2D sample code for an PIC16F87x device.
* This code willl set up the A2D module to return an
* 8-Bit result. If greater precision is needed, a 10-Bit
* result can be returned if read_a2d() is modified to
* return the short ((ADRESH<<8)+(ADRESL)). Note also that
* ADCON1 should be set to 0x80 in the init_a2d() routine
* instead of zero.
*
* This code will sample an A2D value on analog port RA0, and it's value
* will be used to move a LED's position across PORTB.
*
* This project can be demonstrated on the Microchip PICDEM2 board.
*/
__CONFIG(DEBUGEN & WDTDIS & LVPDIS); // Setup the configuration word for ise with ICD2
/* Sample code to set up the A2D module */
void init_a2d(void){
ADCON0=0; // select Fosc/2
ADCON1=0; // select left justify result. A/D port configuration 0
ADON=1; // turn on the A2D conversion module
}
/* Return an 8 bit result */
unsigned char read_a2d(unsigned char channel){
channel&=0x07; // truncate channel to 3 bits
ADCON0&=0xC5; // clear current channel select
ADCON0|=(channel<<3); // apply the new channel select
ADGO=1; // initiate conversion on the selected channel
while(ADGO)continue;
return(ADRESH); // return 8 MSB of the result
}
void main(void){
unsigned char x;
init_a2d(); // initialise the A2D module
GIE=0; // we don't want interrupts
TRISB=0xF0; // the lower four bits of POTRB will be used in output mode
for(;;){
x=read_a2d(1); // sample the analog value on RA0
PORTB = (1<<(x>>6)); // Use the 2 MS Bits of the result to select the bit position of the LED on PORTB
}
}
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante