Skip to content

Commit

Permalink
Refactoring, ADC tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkozlov69 committed Mar 22, 2020
1 parent 7c0f3c5 commit f47f73b
Showing 1 changed file with 47 additions and 50 deletions.
97 changes: 47 additions & 50 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +6,87 @@
#define BITSET(mask) GPIOC->regs->BSRR = mask
#define BITCLR(mask) GPIOC->regs->BRR = mask

#define ADC_PIN PA7
#define CLK_PIN PA8

// Full frame, including dark pixels
// and dead pixels.
#define PIXEL_COUNT 3691

uint8_t buffer[PIXEL_COUNT];
uint8_t buffer2[85];
uint8_t avg = 0;
uint16_t buffer[PIXEL_COUNT];
uint16_t buffer2[85];
uint16_t avg = 0;
int exposureTime = 10
;


void setup()
{
pinMode(PA8, WiringPinMode::PWM);
pinMode(CLK_PIN, WiringPinMode::PWM);
pinMode(ADC_PIN, WiringPinMode::INPUT_ANALOG);

timer_dev *dev = PIN_MAP[PA8].timer_device;
uint8 cc_channel = PIN_MAP[PA8].timer_channel;
timer_dev *t_dev = PIN_MAP[CLK_PIN].timer_device;
uint8 cc_channel = PIN_MAP[CLK_PIN].timer_channel;

timer_set_prescaler(dev, (uint16)(0));
timer_set_reload(dev, 100);
timer_set_compare(dev, cc_channel, 50);
timer_set_prescaler(t_dev, (uint16)(0));
timer_set_reload(t_dev, 100);
timer_set_compare(t_dev, cc_channel, 50);

adc_set_prescaler(ADC_PRE_PCLK2_DIV_2);
adc_set_sample_rate(ADC1, ADC_SMPR_1_5);
adc_set_sample_rate(ADC1, ADC_SMPR_7_5);

GPIOC->regs->CRL = 0x00000000;
GPIOC->regs->CRH = 0x33300000;
}

void readCCD(void)
{
int x;
uint8_t result;

//CLOCK &= ~ICG;
uint16_t result;
BITCLR(ICG);

//_delay_loop_1(12);
delayMicroseconds(1);

//CLOCK |= SH;
BITSET(SH);

BITSET(SH);
delayMicroseconds(5);

//CLOCK &= ~SH;
BITCLR(SH);

delayMicroseconds(15);

//CLOCK |= ICG;
BITSET(ICG);

delayMicroseconds(1);

for (x = 0; x < PIXEL_COUNT; x++)
// analogRead() preparation work
adc_dev *a_dev = PIN_MAP[ADC_PIN].adc_device;
adc_reg_map *adc_regs = a_dev->regs;
adc_set_reg_seqlen(a_dev, 1);
adc_regs->SQR3 = PIN_MAP[ADC_PIN].adc_channel;

for (int x = 0; x < PIXEL_COUNT; x++)
{
//CLOCK |= SH;
BITSET(SH);

result = (uint8_t)(analogRead(PA7) >> 2);

if (x == 0)
{
avg = result;
}
else
{
// TODO data is inverted when we connect ADC directly
if (result < avg)
{
// result = 0;
}
else
{
// result -= avg;
}

buffer[x] = result;
delayMicroseconds(5);
}
//result = (uint8_t)(analogRead(PA7) >> 2);
adc_regs->CR2 |= ADC_CR2_SWSTART;
while (!(adc_regs->SR & ADC_SR_EOC))
;
result = (uint16)(adc_regs->DR & ADC_DR_DATA);

// if (x == 0)
// {
// avg = result;
// }
// else
// {
// // TODO data is inverted when we connect ADC directly
// if (result > avg)
// {
// result = 0;
// }
// else
// {
// result += avg;
// }
// }

buffer[x] = result;
delayMicroseconds(5);

//CLOCK &= ~SH;
BITCLR(SH);
}
}
Expand Down

0 comments on commit f47f73b

Please sign in to comment.