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

📄 test8.c

📁 hrisc cpu
💻 C
字号:
// Test8
//
// This tests out the risc16f84 to see if it can service interrupts.
// In particular, the interrupts will occur during subroutines, so
// the stack will be tested beyond one level.
// Periodic interrupts increment a byte counter, which is displayed
// on the LEDs, while the colored boxes are being printed to the LCD
// panel.
//
// Author: John Clayton
// Date  : 11/25/02

#device PIC16C84
#byte stat_reg     = 0x03
#byte addr_lo      = 0x05
#byte addr_hi      = 0x06
#byte data         = 0x08
#byte w_temp       = 0x0c
#byte stat_temp    = 0x0d
#byte addr_lo_temp = 0x0e
#byte addr_hi_temp = 0x0f

// Global type variables
int int_count;

// This functions plots a single pixel at coordinates (x,y)
// color should range from 0..7.
void plot(unsigned int x, unsigned int y, unsigned int color)
{
addr_hi = (y>>1) | 0x40;
addr_lo = x | (y<<7);
data = color;
}

void fill_block(unsigned int x1,
                unsigned int y1,
                unsigned int x2,
                unsigned int y2,
                unsigned int color)
{
int i,j;

for (i=x1;i<x2;i++)
  for (j=y1;j<y2;j++)
    plot (i,j,color);
}

void show_leds(unsigned int value)
{
addr_hi = 0xff;
addr_lo = 0x07;
data = value;
}

// This is an interrupt service routine
// #int_default -- this directive causes the compiler to produce
// code for saving registers and "cleanup."  It is not working
// too well.

#int_global
void isr1()
{
  // Save the 'w' register, so that the ISR will not destroy
  // any work in progress using the 'w' register at the time
  // the interrupt occurred.
  // Also, save the status register
  // Also, save the aux address bus values
  #asm
    movwf w_temp
    movf  stat_reg,w
    movwf stat_temp
    movf  addr_hi,w
    movwf addr_hi_temp
    movf  addr_lo,w
    movwf addr_lo_temp
  #endasm
  show_leds(++int_count);
  // Restore the contents of the 'w' register and status register
  #asm
    movf  addr_lo_temp,w
    movwf addr_lo
    movf  addr_hi_temp,w
    movwf addr_hi
    movf  stat_temp,w
    movwf stat_reg
    movf  w_temp,w
  #endasm
}

int pn_sequence(unsigned int value)
// Takes a PN sequence value, and produces the next value...
{
return((value << 1) + (((value>>2) ^ (value>>7))&0x01));
}

main() {
int rand;
int a, x1,x2,y1,y2,color,temp;
long b;

// Clear the screen
  fill_block(0,0,128,96,0);

// Initialize the PN generator with a seed...
rand = 0xf0;

// Enable the interrupts
  #asm

    bsf 0x0b,4        // set the interrupt flag
    bsf 0x0b,7        // set the GIE
  #endasm

while (1) {
  rand = pn_sequence(rand);
  x1 = rand;
  rand = pn_sequence(rand);
  x2 = rand;
  rand = pn_sequence(rand);
  y1 = rand;
  rand = pn_sequence(rand);
  y2 = rand;
  rand = pn_sequence(rand);
  color = rand;
  x1 = x1 >> 1;
  x2 = x2 >> 1;
  // Implement various checks to ensure that the coordinates
  // are properly sized for drawing rectangles.
  if (y1 > 95) y1 = 85;
  if (y2 > 95) y2 = 95;
  if (x1 > 126) x1 = 1;
  if (x2 > 126) x2 = 120;
  if (x1 == x2) x2+=10;
  else if (x1>x2) {
	  temp = x1;
	  x1 = x2;
	  x2 = temp;
  }
  if (y1 == y2) y2+= 12;
  else if (y1>y2) {
	  temp = y1;
	  y1 = y2;
	  y2 = temp;
  }
  fill_block(x1,y1,x2,y2,color);
  // Delay for debug
  for (a=0;a<8;a++)
    for (b=0;b<16000;b++) {}

} // End of while loop

} // End of main()

⌨️ 快捷键说明

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