⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drawlib.c

📁 compiled for AtMega32 The source, to compile with AVR-G
💻 C
字号:
//
// Title        : drawlib - part of scopetris
// Author       : Lars Pontoppidan 
// Date         : Aug. 2007
// Version      : 0.1
// Target MCU   : AtMega32 at 8 MHz
//
// DESCRIPTION:
// ------------
// Pen functions for controlling the vector x and y coordinate with a 
// deterministic speed.
//
// timer0 is used for timing the movement.
// 
// The basic principle is to control the movement speed of the pen to control
// boldness of the line. In order to allow fast enough movement, the pen 
// movement does NOT use interrupts. The CPU is locked during pen movement.
// 
//
// DISCLAIMER:
// The author is in no way responsible for any problems or damage caused by
// using this code. Use at your own risk.
//
// LICENSE:
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//

#include <avr/io.h>
#include <avr/interrupt.h>
#include "drawlib.h"


#define PORTX PORTD
#define PORTY PORTC
#define DDRX  DDRD
#define DDRY  DDRC


inline void wait_pixel(void)
{
  while((TIFR & (1<<OCF0)) == 0);
  TIFR = (1<<OCF0); // write logic one to clear OCF0
}


void draw_set_xy(unsigned char x, unsigned char y) 
{
  PORTX = x;
  PORTY = y;  
}

void draw_pen_dxdy(unsigned char dx, unsigned char dy, unsigned char length)
{
  do {
    wait_pixel();
    PORTX = PORTX + dx;
    PORTY = PORTY + dy;
  } while (--length != 0);
}


void draw_pen_dx(unsigned char dx, unsigned char length)
{
  do {
    wait_pixel();
    PORTX = PORTX + dx;
  } while (--length != 0);
}

void draw_pen_dy(unsigned char dy, unsigned char length)
{
  do {
    wait_pixel();
    PORTY = PORTY + dy;
  } while (--length != 0);
}

// Assumes x2>x1 and y2>y1
void draw_box(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
{
  draw_set_xy(x1,y1);   
  draw_pen_dx(1, x2-x1);   
  draw_pen_dy(1, y2-y1); 
  draw_pen_dx(-1, x2-x1);   
  draw_pen_dy(-1, y2-y1);  
}


void draw_init(void)
{
  // timer0 as pixelclock
  
  // Make timer0 set interrupt flags, no prescaler
  // Compare at 40 gives 20*0.125us = 2.5 us
    
  // CTC mode: WGM0 = 2
  TCCR0 = (1<<WGM01) | (1<<CS00);
  TCNT0 = 0;
  OCR0 = 20;  // PIXEL_SPEED
  
  // Setup ports
  draw_set_xy(0,0);
  DDRX = 0xFF;
  DDRY = 0xFF;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -