I am using HT66F0021 controller interfacing with adc. I am new to this controller.I am not able to read the ADC value, my condition is not becoming true and I am not having uart to print the data. I have attached my code for reference. I am using AN2 channel.
Mod: please use Code display option, top on this post menu bar ....
Mod: please use Code display option, top on this post menu bar ....
C-like:
#include "HT66F0021.h"
void Delay_ms(unsigned int count);
void McuConfig(void);
void GpioConfig(void);
void AdcConfig(void);
void AdcMeasure(void);
unsigned int AdcResult = 0;
void main()
{
McuConfig();
while (1)
{
AdcMeasure(); // Update AdcResult with the current ADC value
if (AdcResult > 512) // Compare ADC result
{
_pa5 = 1; // Set PA5 high
}
else
{
_pa5 = 0; // Set PA5 low
}
Delay_ms(100); // Delay for stability
}
}
void Delay_ms(unsigned int count)
{
char i, j;
for (j = 0; j < count; j++)
{
i = 100;
while (i)
{
_clrwdt(); // Clear watchdog timer
_nop(); // No operation (multiple for timing adjustment)
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
_nop();
i--;
}
}
}
void McuConfig(void)
{
GpioConfig();
AdcConfig();
}
void GpioConfig(void)
{
_pac = 0b10011011; // Configure port A as needed
_pa5 = 0; // Set PA5 to low initially
_pac5 = 0; // Configure PA5 as output
}
void AdcConfig(void)
{
_sadol = 0; // Clear ADC data low byte
_sadoh = 0; // Clear ADC data high byte
_sadc0 = 0b00010000; // Configure ADC settings (e.g., clock, mode)
_sadc1 = 0b00000100; // Enable ADC and set input channel
_pasr = 0b01100000; // Select ADC channel (e.g., PA5 as input)
}
void AdcMeasure(void)
{
_start = 1; // Start ADC conversion
while (_adbz); // Wait for ADC conversion to complete
_start = 0; // Reset the start bit
// Read ADC result
AdcResult = (_sadoh << 8) | _sadol; // Combine high and low bytes
//AdcResult = (_sadoh << 2) | (_sadol >> 6); // Combine high and low bytes
}
Attachments
-
2.1 KB Views: 4
Last edited by a moderator: