HEF4067BP 16-channel analogue multiplexer/demultiplexer

HEF4067BP 16-channel analogue multiplexer/demultiplexer
Çeşme:
http://labdegaragem.com/forum/topics/bateria-eletr-nica
int selectPins[] = {4,5,6,7}; int count = 0; void setup() { Serial.begin(9600); for (int i = 0; i < 4; i++) { pinMode(selectPins[i], OUTPUT); } } void loop() { for (count = 0; count < 4; count++) { for (int i = 0; i < 4; i++) { digitalWrite(selectPins[i], bitRead(count, i)); } int val = analogRead(A0); Serial.print(val); Serial.print("\t"); } Serial.println(); delay(200); }
#define F_CPU 16000000UL #include <avr/io.h> #include <stdio.h> #include <util/delay.h> #include <string.h> #define BAUD 9600 void USART_setup(void); void USART_TransmitString(char str[]); void USART_TransmitChar(unsigned char data); void setupADC(); uint16_t readADCValue(uint8_t ch); long map(long x, long in_min, long in_max, long out_min, long out_max); int main(void) { DDRD |= (1<<DDD7)|(1<<DDD6)|(1<<DDD5)|(1<<DDD4); int selectPins[] = {PIND4,PIND5,PIND6,PIND7}; int count = 0; uint16_t value; setupADC(); USART_setup(); char temp[50]; while (1) { for (count = 0; count < 4; count++) { for (int i = 0; i < 4; i++) { if (count & (1 << i)) { PORTD |= (1<<selectPins[i]); } else { PORTD &= ~(1 << selectPins[i]); } } value = readADCValue(PINC0); sprintf(temp, "%ld\t", map(value, 100, 900, 0, 10)); USART_TransmitString(temp); } sprintf(temp, "\n"); USART_TransmitString(temp); _delay_ms(100); } return 0; } void USART_TransmitChar(unsigned char data) { // If UDRE0 is one, the buffer is empty, and therefore ready to be written. while (!(UCSR0A & (1<<UDRE0))) {} UDR0 = data; // USART Transmit / Receive Data Buffer } void USART_TransmitString(char str[]) { for (uint8_t i = 0; i < strlen(str); i++) { USART_TransmitChar(str[i]); } } long map(long x, long in_min, long in_max, long out_min, long out_max) { //return x; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } void USART_setup(void) { int speed = ((F_CPU/16/BAUD)-1); // set boud rate UBRR0H = (speed>>8); UBRR0L = speed; UCSR0B = (1<<TXEN0); // Bit 3 – TXEN0: Transmitter Enable 0 UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // USART Character Size / Data Order = 8-bit } void setupADC() { ADMUX = (1<<REFS0); // For Aref=AVcc; ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //Rrescalar div factor =128 } uint16_t readADCValue(uint8_t ch) { //Select ADC Channel ch must be 0-7 ADMUX |= ch & 0b00000111; //Start Single conversion ADCSRA |= (1<<ADSC); //Wait for conversion to complete while(!(ADCSRA & (1<<ADIF))); //Clear ADIF by writing one to it //Note you may be wondering why we have write one to clear it //This is standard way of clearing bits in io as said in datasheets. //The code writes '1' but it result in setting bit to '0' !!! ADCSRA|=(1<<ADIF); return(ADC); }
#chip mega328p, 16 #define USART_BAUD_RATE 9600 #define USART_TX_BLOCKING Dim count as Byte Dim adVal as Integer dir portd.4 out dir portd.5 out dir portd.6 out dir portd.7 out Do Forever For count = 0 To 3 portd.4 = count.0 portd.5 = count.1 portd.6 = count.2 portd.7 = count.3 adVal = ReadAD10(AN0) HSerPrint adVal HSerSend(9) Next HSerSend(13) HSerSend(10) Wait 300 ms Loop