93 lines
2.4 KiB
Arduino
93 lines
2.4 KiB
Arduino
|
extern "C" {
|
||
|
void init();
|
||
|
void blink();
|
||
|
float float_test();
|
||
|
|
||
|
uint16_t read_temp(uint8_t port);
|
||
|
}
|
||
|
|
||
|
#define UNKNOWN_PIN 0xFF
|
||
|
|
||
|
uint8_t getPinMode(uint8_t pin)
|
||
|
{
|
||
|
uint8_t bit = digitalPinToBitMask(pin);
|
||
|
uint8_t port = digitalPinToPort(pin);
|
||
|
|
||
|
// I don't see an option for mega to return this, but whatever...
|
||
|
if (NOT_A_PIN == port) return UNKNOWN_PIN;
|
||
|
|
||
|
// Is there a bit we can check?
|
||
|
if (0 == bit) return UNKNOWN_PIN;
|
||
|
|
||
|
// Is there only a single bit set?
|
||
|
if (bit & bit - 1) return UNKNOWN_PIN;
|
||
|
|
||
|
volatile uint8_t *reg, *out;
|
||
|
reg = portModeRegister(port);
|
||
|
out = portOutputRegister(port);
|
||
|
|
||
|
if (*reg & bit)
|
||
|
return OUTPUT;
|
||
|
else if (*out & bit)
|
||
|
return INPUT_PULLUP;
|
||
|
else
|
||
|
return INPUT;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
ADC Prescaler Select Bits (ADPS): ADPS2, ADPS1 and ADPS0 bits are used to set circuit clock frequency.
|
||
|
For ADC circuitry to work at its maximum resolution needs to be supplied with 50 kHz to 200 kHz frequency as per the datasheet;
|
||
|
but the system clock will be generally higher (8 MHz, 10 MHz, 16 MHz etc).
|
||
|
To reduce it to required frequency we use ADC prescaler bits. Suppose we have system clock with frequency 10Mhz (10000000 Hz) and set division factor to 64,
|
||
|
then ADC clock frequency is 10000000/64 = 156250Hz = 156.25 KHz, which is between 50 to 200 KHz as mentioned in the datasheet.
|
||
|
(http://www.robotplatform.com/knowledge/ADC/adc_tutorial_2.html)
|
||
|
|
||
|
16 MHz / 128 = 16000000 Hz / 128 = 125000 Hz = 125 KHz => OK!
|
||
|
*/
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
|
||
|
|
||
|
// init();
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
// blink();
|
||
|
/*ADMUX = 0b01000011;
|
||
|
PRR = 0b00000000;
|
||
|
ADCSRA = 0b11000111;
|
||
|
while ((ADCSRA & 0b01000000) != 0);
|
||
|
short k = ADC;*/
|
||
|
|
||
|
// max value = 1023
|
||
|
// ref voltage is 1.1 V
|
||
|
// 1023 - max byte value
|
||
|
// +2 to 150, 10 mv/C
|
||
|
// k / 255 * 1.1 * 1000 / 10 + 2
|
||
|
// Serial.println((float)k * 0.43137254 + 2);
|
||
|
// pinMode(A3, OUTPUT);
|
||
|
|
||
|
float *f = malloc(sizeof(f));
|
||
|
*f = float_test();
|
||
|
|
||
|
for (int i = 0; i < 4; i++) {
|
||
|
Serial.print(((unsigned char*)f)[i]);
|
||
|
Serial.print(" ");
|
||
|
}
|
||
|
Serial.print(" => ");
|
||
|
Serial.print(*f, 8);
|
||
|
Serial.println();
|
||
|
|
||
|
// uint16_t k = read_temp(A3);
|
||
|
/*Serial.print(getPinMode(A0));
|
||
|
Serial.print(getPinMode(A1));
|
||
|
Serial.print(getPinMode(A2));
|
||
|
Serial.print(getPinMode(A3));
|
||
|
Serial.print(getPinMode(A4));
|
||
|
Serial.print(getPinMode(A5));
|
||
|
Serial.println();*/
|
||
|
// Serial.println((float)k / 1023 * 1.1 * 1000 / 10 + 2);
|
||
|
// Serial.println((float)k * 0.10752688 + 2);
|
||
|
}
|