Skip to content

Instantly share code, notes, and snippets.

@M0nteCarl0
Created August 8, 2023 14:32
Show Gist options
  • Save M0nteCarl0/8dfc4d17ca8cfc9873d5c0a57694d643 to your computer and use it in GitHub Desktop.
Save M0nteCarl0/8dfc4d17ca8cfc9873d5c0a57694d643 to your computer and use it in GitHub Desktop.
DCMI inteface
#include <avr/io.h>
#include <avr/interrupt.h>
#define PIXEL_WIDTH 8
#define IMAGE_WIDTH 640
#define IMAGE_HEIGHT 480
volatile uint8_t pixel_count = 0;
volatile uint8_t line_count = 0;
volatile uint8_t frame_count = 0;
uint8_t image_buffer[IMAGE_HEIGHT][IMAGE_WIDTH];
uint8_t pixel_data = 0;
void setup() {
// Set PORTB pins as output
DDRB = 0xFF;
// Set up Timer1 for pixel clock
TCCR1A = 0;
TCCR1B = (1 << CS10);
TIMSK1 = (1 << TOIE1);
// Enable interrupts
sei();
}
void loop() {
// Your main program logic here
}
ISR(TIMER1_OVF_vect) {
if (pixel_count >= IMAGE_WIDTH) {
pixel_count = 0;
line_count++;
if (line_count >= IMAGE_HEIGHT) {
line_count = 0;
frame_count++;
}
}
if (frame_count == 0 && line_count == 0 && pixel_count == 0) {
// Start of a new frame
// Your code here
} else if (frame_count > 0 && line_count == 0 && pixel_count == 0) {
// Start of a new line
// Your code here
} else {
// Capture pixel data
if (line_count < IMAGE_HEIGHT && pixel_count < IMAGE_WIDTH) {
image_buffer[line_count][pixel_count] = pixel_data;
}
pixel_count++;
}
// Output image data
if (line_count < IMAGE_HEIGHT && pixel_count < IMAGE_WIDTH) {
PORTB = image_buffer[line_count][pixel_count];
} else {
PORTB = 0;
}
}
module DCMI_Controller (
input wire clk,
input wire reset,
input wire dcmi_pclk,
input wire dcmi_vsync,
input wire dcmi_hsync,
input wire [7:0] dcmi_data,
output wire [7:0] image_data
);
parameter PIXEL_WIDTH = 8;
parameter IMAGE_WIDTH = 640;
parameter IMAGE_HEIGHT = 480;
reg [PIXEL_WIDTH-1:0] pixel_count;
reg [8:0] line_count;
reg [8:0] frame_count;
reg [7:0] image_buffer [0:IMAGE_HEIGHT-1][0:IMAGE_WIDTH-1];
reg [7:0] pixel_data;
always @(posedge clk) begin
if (reset) begin
pixel_count <= 0;
line_count <= 0;
frame_count <= 0;
end else begin
if (dcmi_pclk && dcmi_vsync && dcmi_hsync) begin
// Start of a new frame
frame_count <= frame_count + 1;
line_count <= 0;
pixel_count <= 0;
end else if (dcmi_pclk && dcmi_vsync) begin
// Start of a new line
line_count <= line_count + 1;
pixel_count <= 0;
end else if (dcmi_pclk) begin
// Capture pixel data
if (line_count < IMAGE_HEIGHT && pixel_count < IMAGE_WIDTH) begin
image_buffer[line_count][pixel_count] <= dcmi_data;
end
pixel_count <= pixel_count + 1;
end
end
end
always @(posedge clk) begin
if (reset) begin
image_data <= 0;
end else begin
if (line_count < IMAGE_HEIGHT && pixel_count < IMAGE_WIDTH) begin
image_data <= image_buffer[line_count][pixel_count];
end else begin
image_data <= 0;
end
end
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment