// ECE 476 // Final Project // Digital Compass // // Chun C. Wang (CCW27) // Liang J. Zhao (LZ36) // // 4/31/2004 #pragma regalloc- // allocate the registers #pragma optsize- // optimize for speed #include #include #include #include #include //cycles = 63.625 * 16 Note NTSC is 63.55 //but this line duration makes each frame exactly 1/60 sec //which is nice for keeping a realtime clock #define lineTime 1018 // the following are the origina definition by Prof. Land #define begin { #define end } #define ScreenTop 30 #define ScreenBot 230 #define width 126 // for the state machine that controls the ADC // it reads one channel at a time #define Channel0 0 #define Channel1 1 #define Channel2 2 // define the four directions #define North 3 #define East 4 #define South 5 #define West 6 // define the center pixel on the TV screen #define centerx 60 #define centery 45 //NOTE that v1 to v8 and i must be in registers! register char v1 @4; register char v2 @5; register char v3 @6; register char v4 @7; register char v5 @8; register char v6 @9; register char v7 @10; register char v8 @11; register int i @12; #pragma regalloc+ char syncON, syncOFF; // dictates the synch signals int LineCount; // counts the current line being drawn int time; // for software timer // stores the ADC sample values unsigned char sample, sample1, sample2, sample3; // stores old xy values (for erasing the previous trace) unsigned char oldx, oldy; // stores the state, direction, and mode of the digital compass unsigned char state, direction, mode; // for the for-loops unsigned char x1, y1; // store the delay and timer-counter value unsigned char delay, counter; // for the pseudo-debounce while-loop unsigned char keycounter; // stores the user-set magnetic declination int declination; // d and dd are for drawing circles on the screen int d; float dd; // for computing the current direction int diff[4], min_diff, min_index; // stores the direction (in degrees) to be displayed on TV int output; // stores the pixel contents char screen[1600], t, ts[10]; // for display on screen // the V's are modified to display a space char N[]="NV"; char NE[]="NE"; char NW[]="NW"; char SE[]="SE"; char SW[]="SW"; char W[]="WV"; char E[]="EV"; char S[]="SV"; char F[]="F"; char deg[]="O"; char space[]="V"; // stores the user-chosen direction in Alert Mode unsigned char pointer; // Point plot lookup table // One bit masks flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; // position mapping for display (the compass "needle" endpoint) // the y-positions flash unsigned char x[360] = { 60, 60, 61, 61, 62, 62, 63, 63, 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70, 71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 75, 76, 76, 77, 77, 78, 78, 78, 79, 79, 80, 80, 80, 81, 81, 81, 82, 82, 82, 83, 83, 83, 84, 84, 84, 85, 85, 85, 85, 86, 86, 86, 86, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 90, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 89, 88, 88, 88, 88, 88, 88, 88, 87, 87, 87, 87, 86, 86, 86, 86, 85, 85, 85, 85, 84, 84, 84, 83, 83, 83, 82, 82, 82, 81, 81, 81, 80, 80, 80, 79, 79, 78, 78, 78, 77, 77, 76, 76, 75, 75, 75, 74, 74, 73, 73, 72, 72, 71, 71, 70, 70, 69, 69, 68, 68, 67, 67, 66, 66, 65, 65, 64, 64, 63, 63, 62, 62, 61, 61, 60, 60, 59, 58, 58, 57, 57, 56, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 48, 48, 47, 47, 46, 46, 45, 45, 45, 44, 44, 43, 43, 42, 42, 41, 41, 41, 40, 40, 39, 39, 39, 38, 38, 38, 37, 37, 37, 36, 36, 36, 35, 35, 35, 34, 34, 34, 34, 33, 33, 33, 33, 32, 32, 32, 32, 31, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 41, 41, 41, 42, 42, 43, 43, 44, 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 58, 58, 59 }; // position mapping for display (the compass "needle" endpoint) // the y-positions flash unsigned char y[360]= { 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 45, 45, 46, 46, 47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54, 55, 55, 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 60, 61, 61, 62, 62, 63, 63, 63, 64, 64, 65, 65, 65, 66, 66, 66, 67, 67, 67, 68, 68, 68, 69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 71, 72, 72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 75, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 73, 73, 73, 73, 73, 73, 73, 72, 72, 72, 72, 71, 71, 71, 71, 70, 70, 70, 70, 69, 69, 69, 68, 68, 68, 67, 67, 67, 66, 66, 66, 65, 65, 65, 64, 64, 63, 63, 63, 62, 62, 61, 61, 60, 60, 60, 59, 59, 58, 58, 57, 57, 56, 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 48, 48, 47, 47, 46, 46, 45, 45, 44, 43, 43, 42, 42, 41, 41, 40, 40, 39, 39, 38, 38, 37, 37, 36, 36, 35, 35, 34, 34, 33, 33, 32, 32, 31, 31, 30, 30, 29, 29, 29, 28, 28, 27, 27, 26, 26, 26, 25, 25, 24, 24, 24, 23, 23, 23, 22, 22, 22, 21, 21, 21, 20, 20, 20, 19, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 }; // compass mapping for channel 1 (the "cosine" curve) // obtained by calibration flash int chan2[245][2]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 336, 321, 339, 314, 343, 311, 347, 308, 349, 306, 352, 304, 353, 302, 355, 301, 356, 299, 357, 297, 359, 296, 359, 295, 294, 0, 292, 2, 291, 3, 290, 5, 288, 6, 287, 7, 286, 8, 284, 9, 283, 10, 282, 12, 281, 13, 280, 14, 278, 15, 277, 16, 276, 17, 274, 18, 273, 19, 272, 20, 271, 22, 270, 23, 268, 24, 267, 26, 266, 27, 265, 28, 264, 29, 263, 30, 262, 31, 261, 32, 260, 33, 259, 34, 258, 35, 257, 36, 256, 37, 255, 38, 254, 39, 253, 39, 252, 40, 251, 42, 250, 43, 248, 44, 247, 45, 245, 46, 244, 48, 242, 49, 241, 50, 240, 52, 238, 53, 237, 55, 236, 56, 235, 58, 234, 59, 233, 60, 232, 62, 231, 63, 230, 65, 228, 66, 227, 68, 226, 69, 225, 70, 224, 72, 223, 73, 222, 75, 221, 76, 220, 78, 218, 79, 217, 80, 215, 82, 214, 84, 212, 85, 211, 87, 210, 89, 208, 90, 207, 92, 205, 94, 204, 95, 203, 97, 201, 99, 200, 100, 198, 102, 196, 104, 194, 106, 192, 108, 190, 110, 187, 113, 184, 115, 182, 118, 180, 120, 177, 123, 175, 127, 173, 130, 170, 135, 160, 140, 150, 149 }; // compass mapping for channel 2 (the "sine" curve) // obtained by calibration flash int chan1[244][2]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 31, 49, 25, 59, 21, 64, 17, 67, 15, 69, 13, 72, 11, 75, 9, 77, 7, 79, 5, 82, 4, 85, 2, 87, 1, 359, 0, 358, 90, 357, 93, 356, 95, 355, 96, 354, 98, 353, 100, 352, 103, 351, 105, 350, 107, 349, 109, 348, 110, 347, 112, 346, 113, 345, 114, 344, 116, 343, 117, 342, 118, 341, 119, 340, 120, 340, 122, 339, 124, 338, 125, 337, 127, 336, 129, 335, 130, 335, 132, 334, 134, 333, 135, 332, 137, 331, 138, 330, 139, 330, 140, 329, 142, 328, 143, 327, 144, 326, 145, 325, 147, 324, 148, 323, 149, 322, 150, 321, 152, 320, 153, 320, 155, 319, 156, 318, 158, 317, 159, 316, 160, 315, 162, 314, 163, 313, 165, 312, 166, 311, 168, 310, 169, 310, 170, 309, 172, 308, 173, 307, 174, 306, 175, 305, 177, 304, 178, 303, 179, 302, 180, 301, 182, 300, 183, 298, 185, 297, 186, 296, 188, 295, 189, 294, 190, 293, 192, 292, 193, 291, 195, 290, 196, 288, 198, 287, 199, 286, 200, 284, 202, 283, 204, 281, 205, 280, 207, 278, 209, 276, 210, 274, 213, 272, 215, 270, 218, 267, 220, 265, 224, 262, 227, 260, 230, 256, 235, 250, 240 }; //define some character bitmaps //5x7 characters flash char bitmap[38][7]={ //0 0b01110000, 0b10001000, 0b10011000, 0b10101000, 0b11001000, 0b10001000, 0b01110000, //1 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //2 0b01110000, 0b10001000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b11111000, //3 0b11111000, 0b00010000, 0b00100000, 0b00010000, 0b00001000, 0b10001000, 0b01110000, //4 0b00010000, 0b00110000, 0b01010000, 0b10010000, 0b11111000, 0b00010000, 0b00010000, //5 0b11111000, 0b10000000, 0b11110000, 0b00001000, 0b00001000, 0b10001000, 0b01110000, //6 0b01000000, 0b10000000, 0b10000000, 0b11110000, 0b10001000, 0b10001000, 0b01110000, //7 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b10000000, //8 0b01110000, 0b10001000, 0b10001000, 0b01110000, 0b10001000, 0b10001000, 0b01110000, //9 0b01110000, 0b10001000, 0b10001000, 0b01111000, 0b00001000, 0b00001000, 0b00010000, //A 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, //B 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10001000, 0b10001000, 0b11110000, //C 0b01110000, 0b10001000, 0b10000000, 0b10000000, 0b10000000, 0b10001000, 0b01110000, //D 0b11110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11110000, //E 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b11111000, //F 0b11111000, 0b10000000, 0b10000000, 0b11111000, 0b10000000, 0b10000000, 0b10000000, //G 0b01110000, 0b10001000, 0b10000000, 0b10011000, 0b10001000, 0b10001000, 0b01110000, //H 0b10001000, 0b10001000, 0b10001000, 0b11111000, 0b10001000, 0b10001000, 0b10001000, //I 0b01110000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b01110000, //J 0b00111000, 0b00010000, 0b00010000, 0b00010000, 0b00010000, 0b10010000, 0b01100000, //K 0b10001000, 0b10010000, 0b10100000, 0b11000000, 0b10100000, 0b10010000, 0b10001000, //L 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b11111000, //M 0b10001000, 0b11011000, 0b10101000, 0b10101000, 0b10001000, 0b10001000, 0b10001000, //N 0b10001000, 0b10001000, 0b11001000, 0b10101000, 0b10011000, 0b10001000, 0b10001000, //O 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //P 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10000000, 0b10000000, 0b10000000, //Q 0b01110000, 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10010000, 0b01101000, //R 0b11110000, 0b10001000, 0b10001000, 0b11110000, 0b10100000, 0b10010000, 0b10001000, //S 0b01111000, 0b10000000, 0b10000000, 0b01110000, 0b00001000, 0b00001000, 0b11110000, //T 0b11111000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, 0b00100000, //U 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b01110000, //V 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //W 0b10001000, 0b10001000, 0b10001000, 0b10101000, 0b10101000, 0b10101000, 0b01010000, //X 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b01010000, 0b10001000, 0b10001000, //Y 0b10001000, 0b10001000, 0b10001000, 0b01010000, 0b00100000, 0b00100000, 0b00100000, //Z 0b11111000, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000, 0b11111000, //figure1 0b01110000, 0b00100000, 0b01110000, 0b10101000, 0b00100000, 0b01010000, 0b10001000, //figure2 0b01110000, 0b10101000, 0b01110000, 0b00100000, 0b00100000, 0b01010000, 0b10001000}; //================================ //3x5 font numbers, then letters //packed two per definition for fast //copy to the screen at x-position divisible by 4 flash char smallbitmap[39][5]={ //0 0b11101110, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //1 0b01000100, 0b11001100, 0b01000100, 0b01000100, 0b11101110, //2 0b11101110, 0b00100010, 0b11101110, 0b10001000, 0b11101110, //3 0b11101110, 0b00100010, 0b11101110, 0b00100010, 0b11101110, //4 0b10101010, 0b10101010, 0b11101110, 0b00100010, 0b00100010, //5 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //6 0b11001100, 0b10001000, 0b11101110, 0b10101010, 0b11101110, //7 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b10001000, //8 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b11101110, //9 0b11101110, 0b10101010, 0b11101110, 0b00100010, 0b01100110, //: 0b00000000, 0b01000100, 0b00000000, 0b01000100, 0b00000000, //= 0b00000000, 0b11101110, 0b00000000, 0b11101110, 0b00000000, //blank 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //A 0b11101110, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //B 0b11001100, 0b10101010, 0b11101110, 0b10101010, 0b11001100, //C 0b11101110, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //D 0b11001100, 0b10101010, 0b10101010, 0b10101010, 0b11001100, //E 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b11101110, //F 0b11101110, 0b10001000, 0b11101110, 0b10001000, 0b10001000, //G 0b11101110, 0b10001000, 0b10001000, 0b10101010, 0b11101110, //H 0b10101010, 0b10101010, 0b11101110, 0b10101010, 0b10101010, //I 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b11101110, //J 0b00100010, 0b00100010, 0b00100010, 0b10101010, 0b11101110, //K 0b10001000, 0b10101010, 0b11001100, 0b11001100, 0b10101010, //L 0b10001000, 0b10001000, 0b10001000, 0b10001000, 0b11101110, //M 0b10101010, 0b11101110, 0b11101110, 0b10101010, 0b10101010, //N 0b00000000, 0b11001100, 0b10101010, 0b10101010, 0b10101010, //O 0b01000100, 0b10101010, 0b10101010, 0b10101010, 0b01000100, //P 0b11101110, 0b10101010, 0b11101110, 0b10001000, 0b10001000, //Q 0b01000100, 0b10101010, 0b10101010, 0b11101110, 0b01100110, //R 0b11101110, 0b10101010, 0b11001100, 0b11101110, 0b10101010, //S 0b11101110, 0b10001000, 0b11101110, 0b00100010, 0b11101110, //T 0b11101110, 0b01000100, 0b01000100, 0b01000100, 0b01000100, //U 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b11101110, //V 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, //W 0b10101010, 0b10101010, 0b11101110, 0b11101110, 0b10101010, //X 0b00000000, 0b10101010, 0b01000100, 0b01000100, 0b10101010, //Y 0b10101010, 0b10101010, 0b01000100, 0b01000100, 0b01000100, //Z 0b11101110, 0b00100010, 0b01000100, 0b10001000, 0b11101110 }; //================================== // This is the sync generator and raster generator. It MUST be entered from // sleep mode to get accurate timing of the sync pulses // modified from Prof. Land's code #pragma warn- interrupt [TIM1_COMPA] void t1_cmpA(void) begin //start the Horizontal sync pulse PORTD = syncON; //update the curent scanline number LineCount ++ ; //begin inverted (Vertical) synch after line 247 if (LineCount==248) begin syncON = 0b00100000; syncOFF = 0; end //back to regular sync after line 250 if (LineCount==251) begin syncON = 0; syncOFF = 0b00100000; end //start new frame after line 262 if (LineCount==263) begin LineCount = 1; end delay_us(2); //adjust to make 5 us pulses //end sync pulse PORTD = syncOFF; ADCSR.6 = 1 ; if (LineCount=ScreenTop) begin //compute byte index for beginning of the next line //left-shift 4 would be individual lines // <<3 means line-double the pixels //The 0xfff8 truncates the odd line bit //i=(LineCount-ScreenTop)<<3 & 0xfff8; // #asm push r16 lds r12, _LineCount lds r13, _Linecount+1 ldi r16, 30 sub r12, r16 ldi r16,0 sbc r13, r16 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 mov r16,r12 andi r16,0xf0 mov r12,r16 pop r16 #endasm //load 16 registers with screen info #asm push r14 push r15 push r16 push r17 push r18 push r19 push r26 push r27 ldi r26,low(_screen) ;base address of screen ldi r27,high(_screen) add r26,r12 ;offset into screen (add i) adc r27,r13 ld r4,x+ ;load 16 registers and inc pointer ld r5,x+ ld r6,x+ ld r7,x+ ld r8,x+ ld r9,x+ ld r10,x+ ld r11,x+ ld r12,x+ ld r13,x+ ld r14,x+ ld r15,x+ ld r16,x+ ld r17,x+ ld r18,x+ ld r19,x pop r27 pop r26 #endasm delay_us(4); //adjust to center image on screen //blast 16 bytes to the screen #asm ;but first a macro to make the code shorter ;the macro takes a register number as a parameter ;and dumps its bits serially to portD.6 ;the nop can be eliminated to make the display narrower .macro videobits ;regnum BST @0,7 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,6 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,5 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,4 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,3 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,2 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,1 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 BST @0,0 IN R30,0x12 BLD R30,6 nop OUT 0x12,R30 .endm videobits r4 ;video line -- byte 1 videobits r5 ;byte 2 videobits r6 ;byte 3 videobits r7 ;byte 4 videobits r8 ;byte 5 videobits r9 ;byte 6 videobits r10 ;byte 7 videobits r11 ;byte 8 videobits r12 ;byte 9 videobits r13 ;byte 10 videobits r14 ;byte 11 videobits r15 ;byte 12 videobits r16 ;byte 13 videobits r17 ;byte 14 videobits r18 ;byte 15 videobits r19 ;byte 16 clt ;clear video after the last pixel on the line IN R30,0x12 BLD R30,6 OUT 0x12,R30 pop r19 pop r18 pop r17 pop r16 pop r15 pop r14 #endasm end sample = ADCH; end #pragma warn+ //================================== // plot one point // at x,y with color 1=white 0=black 2=invert // from Prof. Land #pragma warn- void video_pt(char x, char y, char c) begin #asm ; i=(x>>3) + ((int)y<<4) ; the byte with the pixel in it push r16 ldd r30,y+2 ;get x lsr r30 lsr r30 lsr r30 ;divide x by 8 ldd r12,y+1 ;get y lsl r12 ;mult y by 16 clr r13 lsl r12 rol r13 lsl r12 rol r13 lsl r12 rol r13 add r12, r30 ;add in x/8 ;v2 = screen[i]; r5 ;v3 = pos[x & 7]; r6 ;v4 = c r7 ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 ld r5,Z ;get screen byte ldd r26,y+2 ;get x ldi r27,0 andi r26,0x07 ;form x & 7 ldi r30,low(_pos*2) ldi r31,high(_pos*2) add r30,r26 adc r31,r27 lpm r6,Z ld r16,y ;get c ;if (v4==1) screen[i] = v2 | v3 ; ;if (v4==0) screen[i] = v2 & ~v3; ;if (v4==2) screen[i] = v2 ^ v3 ; cpi r16,1 brne tst0 or r5,r6 tst0: cpi r16,0 brne tst2 com r6 and r5,r6 tst2: cpi r16,2 brne writescrn eor r5,r6 writescrn: ldi r30,low(_screen) ldi r31,high(_screen) add r30, r12 adc r31, r13 st Z, r5 ;write the byte back to the screen pop r16 #endasm end #pragma warn+ //================================== // put a big character on the screen // c is index into bitmap // from Prof. Land void video_putchar(char x, char y, char c) begin v7 = x; for (v6=0;v6<7;v6++) begin v1 = bitmap[c][v6]; v8 = y+v6; video_pt(v7, v8, (v1 & 0x80)==0x80); video_pt(v7+1, v8, (v1 & 0x40)==0x40); video_pt(v7+2, v8, (v1 & 0x20)==0x20); video_pt(v7+3, v8, (v1 & 0x10)==0x10); video_pt(v7+4, v8, (v1 & 0x08)==0x08); end end //================================== // put a string of big characters on the screen // from Prof. Land void video_puts(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_putchar(x,y,str[i]-0x30); else video_putchar(x,y,str[i]-0x40+9); x = x+6; end end //================================== // put a small character on the screen // x-cood must be on divisible by 4 // c is index into bitmap // from Prof. Land void video_smallchar(char x, char y, char c) begin char mask; i=((int)x>>3) + ((int)y<<4) ; if (x == (x & 0xf8)) mask = 0x0f; //f8 else mask = 0xf0; screen[i] = (screen[i] & mask) | (smallbitmap[c][0] & ~mask); screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask); screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask); screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask); screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask); end //================================== // put a string of small characters on the screen // x-cood must be on divisible by 4 // from Prof. Land void video_putsmalls(char x, char y, char *str) begin char i ; for (i=0; str[i]!=0; i++) begin if (str[i]>=0x30 && str[i]<=0x3a) video_smallchar(x,y,str[i]-0x30); else video_smallchar(x,y,str[i]-0x40+12); x = x+4; end end //================================== // plot a line // at x1,y1 to x2,y2 with color 1=white 0=black 2=invert // NOTE: this function requires signed chars // Code is from David Rodgers, // "Procedural Elements of Computer Graphics",1985 // from Prof. Land void video_line(char x1, char y1, char x2, char y2, char c) begin int e; signed char dx,dy,j, temp; signed char s1,s2, xchange; signed char x,y; x = x1; y = y1; dx = cabs(x2-x1); dy = cabs(y2-y1); s1 = csign(x2-x1); s2 = csign(y2-y1); xchange = 0; if (dy>dx) begin temp = dx; dx = dy; dy = temp; xchange = 1; end e = ((int)dy<<1) - dx; for (j=0; j<=dx; j++) begin video_pt(x,y,c) ; if (e>=0) begin if (xchange==1) x = x + s1; else y = y + s2; e = e - ((int)dx<<1); end if (xchange==1) y = y + s2; else x = x + s1; e = e + ((int)dy<<1); end end //================================== // return the value of one point // at x,y with color 1=white 0=black 2=invert // from Prof. Land char video_set(char x, char y) begin //The following construction //detects exactly one bit at the x,y location i=((int)x>>3) + ((int)y<<4) ; return ( screen[i] & 1<<(7-(x & 0x7))); end void main(void) begin //init timer 1 to generate sync OCR1A = lineTime; //One NTSC line TCCR1B = 9; //full speed; clear-on-match TCCR1A = 0x00; //turn off pwm and oc lines TIMSK = 0x10; //enable interrupt T1 cmp // init ADC ADMUX = 0b01100000; ADCSR = 0b11000111; //init ports DDRD = 0xf0; //video out and switches //set up the ports DDRC=0x00; // PORT C is an input DDRB=0xff; // PORT B is an ouput PORTB=0xff; //initialize synch constants LineCount = 1; syncON = 0b00000000; syncOFF = 0b00100000; // create two circles on the screen for(x1=centerx; x1<=100; x1++) { for(y1=centery; y1<=90; y1++) { dd = ((long)(x1-centerx)*(x1-centerx)+(long)(y1-centery)*(y1-centery)); d = (int)sqrt(dd); if (((d < 40) && (d>38))||((d < 34) && (d>32))) { video_pt(x1,y1,1); video_pt(centerx-(x1-centerx),y1,1); video_pt(x1,centery-(y1-centery),1); video_pt(centerx-(x1-centerx),centery-(y1-centery),1); } } } // for the temperature display video_putsmalls(20,92,F); //init software timer t=0; time=0; // initialize direction and pointer to North direction = 0; pointer = 0; // initialize magnetic declination to 11 degree West // this is the declination for Ithaca, NY declination = -11; //use OC0 (pin B.3) for music DDRB.3 = 1 ; TCCR0 = 0b00011100; //enable sleep mode MCUCR = 0b10000000; // start from channel0 state = Channel0; #asm ("sei"); //The following loop executes once/video line during lines //1-230, then does all of the frame-end processing while(1) begin // stall here until next line starts // sleep enable; mode=idle // use sleep to make entry into sync ISR uniform time #asm ("sleep"); // The following code executes during the vertical blanking // Code here can be as long as // a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec if (LineCount==231) { // obtain sample data from ADC switch(state) { case 0: // from the "cosine" curve ADMUX = 0b01100001; sample1 = sample; // display is for testing only sprintf(ts,"%i",sample); video_putsmalls(108,20,ts); state = Channel1; break; case 1: // from the "sine" curve ADMUX = 0b01100010; sample2 = sample; // display is for testing only sprintf(ts,"%i",sample); video_putsmalls(108,40,ts); state = Channel2; break; case 2: // from the temperature sensor ADMUX = 0b01100000; sample3 = sample; sprintf(ts,"%i",sample); video_putsmalls(8,92,ts); state = Channel0; break; } // obtain the difference between the mappings diff[0] = abs(chan1[sample1-1][0] - chan2[sample2-1][0]); diff[1] = abs(chan1[sample1-1][0] - chan2[sample2-1][1]); diff[2] = abs(chan1[sample1-1][1] - chan2[sample2-1][0]); diff[3] = abs(chan1[sample1-1][1] - chan2[sample2-1][1]); // if the diffence if close to 360, the circle // might have been wrapped around for(x1=0; x1<4; x1++) { if (diff[x1]>350) { // take its distance (in degrees) from 0 degree diff[x1] = 360 - diff[x1]; } } // temperarily stores the smallest diffence min_diff = diff[0]; min_index = 0; // calculate the smallest difference for (i = 1; i<4; i++) { if(diff[i] <= min_diff) { min_diff = diff[i]; min_index = i%2; } } /* pseudo-debounce */ while(keycounter >= 10) { // reset the counter keycounter = 0; // if pin 5 is pressed // the declination is increasing toward west if(PINC.5 == 0) { if (declination > -40) { declination = declination - 1; } } // if pin 4 is pressed // the declination is increasing toward east else if(PINC.4 == 0) { if (declination < 40) { declination = declination + 1; } } } // end while // increment counter keycounter++; /* erase the previous trace first */ if ((declination < 10) && (declination > -10)) { video_putsmalls(112,92,space); } // if declination is 0, display 0 if (declination == 0) { video_putsmalls(100,92,space); sprintf(ts,"%-i",-declination); video_putsmalls(108,92,ts); } // if declination is to the west, display W else if (declination < 0) { video_putsmalls(100,92,W); sprintf(ts,"%-i",-declination); video_putsmalls(108,92,ts); } // if declination is to the east, display E else { video_putsmalls(100,92,E); sprintf(ts,"%-i",declination); video_putsmalls(108,92,ts); } // obtain output from the mapping array output = chan2[sample2-1][min_index]; // adjust output by adding or subtracting the // magnetic declination if ((output + declination) < 0) { output = 360 + output + declination; } else if ((output + declination) >= 360) { output = output + declination - 360; } else { output = output + declination; } // display current direction in degree sprintf(ts,"%-i",output); if(output < 10) { video_puts(46,92,space); } if (output < 100) { video_puts(52,92,space); } video_puts(40,92,ts); video_line(centerx,centery,oldx,oldy,0); video_line(centerx,centery,x[output],y[output],1); oldx=x[output]; oldy=y[output]; // assign cardinal point according to the // measured direction (in degrees) if((output < 22) || (output > 338)) { video_puts(68,92,N); pointer = 1; } else if ((output > 23) && (output < 68)) { video_puts(68,92,NE); pointer = 2; } else if ((output > 69) && (output < 114)) { video_puts(68,92,E); pointer = 3; } else if ((output > 115) && (output < 160)) { video_puts(68,92,SE); pointer = 4; } else if ((output > 161) && (output < 206)) { video_puts(68,92,S); pointer = 5; } else if ((output > 207) && (output < 252)) { video_puts(68,92,SW); pointer = 6; } else if ((output > 253) && (output < 298)) { video_puts(68,92,W); pointer = 7; } else if ((output > 299) && (output < 337)) { video_puts(68,92,NW); pointer = 8; } // the user chosen direction if((mode == 1) && (PINC.0 == 0)) { direction = North; } else if((mode == 1) && (PINC.1 == 0)) { direction = East; } else if((mode == 1) && (PINC.2 == 0)) { direction = South; } else if((mode == 1) && (PINC.3 == 0)) { direction = West; } // if pin 7 is pressed, display four cardinal points if(PINC.7 == 0) { mode = 0; // North pt video_pt(60,45-37,0); video_pt(60,45-36,0); video_pt(60,45-35,0); // South pt video_pt(60,45+37,0); video_pt(60,45+36,0); video_pt(60,45+35,0); // East pt video_pt(60+37,45,0); video_pt(60+36,45,0); video_pt(60+35,45,0); // West pt video_pt(60-37,45,0); video_pt(60-36,45,0); video_pt(60-35,45,0); } // if pin 6 is pressed, remove the cardinal points if(PINC.6 == 0) { mode = 1; direction = 0; // North pt video_pt(60,45-37,1); video_pt(60,45-36,1); video_pt(60,45-35,1); // South pt video_pt(60,45+37,1); video_pt(60,45+36,1); video_pt(60,45+35,1); // East pt video_pt(60+37,45,1); video_pt(60+36,45,1); video_pt(60+35,45,1); // West pt video_pt(60-37,45,1); video_pt(60-36,45,1); video_pt(60-35,45,1); } // if in the alert mode if (mode == 1) { // first look at what direction the user chose switch(direction) { case 3: // North if (delay == 15) // flash the point { // North pt video_pt(60,45-37,2); video_pt(60,45-36,2); video_pt(60,45-35,2); delay = 0; // South pt video_pt(60,45+37,1); video_pt(60,45+36,1); video_pt(60,45+35,1); // East pt video_pt(60+37,45,1); video_pt(60+36,45,1); video_pt(60+35,45,1); // West pt video_pt(60-37,45,1); video_pt(60-36,45,1); video_pt(60-35,45,1); } delay++; // increment counter // play different sounds as the needle // points to differnt directions switch (pointer) { case 1: if(counter >= 3) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 25; counter = 0; } counter++; break; case 2: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; case 3: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 4: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 5: if(counter >= 43) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 65; counter = 0; } counter++; break; case 6: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 7: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 8: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; } break; case 4: // West if (delay == 15) { // East pt video_pt(60+37,45,2); video_pt(60+36,45,2); video_pt(60+35,45,2); delay = 0; // North pt video_pt(60,45-37,1); video_pt(60,45-36,1); video_pt(60,45-35,1); // South pt video_pt(60,45+37,1); video_pt(60,45+36,1); video_pt(60,45+35,1); // West pt video_pt(60-37,45,1); video_pt(60-36,45,1); video_pt(60-35,45,1); } delay++; switch (pointer) { case 1: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 25; counter = 0; } counter++; break; case 2: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; case 3: if(counter >= 3) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 4: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 5: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 65; counter = 0; } counter++; break; case 6: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 7: if(counter >= 43) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 8: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; } break; case 5: // South if (delay == 15) { // South pt video_pt(60,45+37,2); video_pt(60,45+36,2); video_pt(60,45+35,2); delay = 0; // North pt video_pt(60,45-37,1); video_pt(60,45-36,1); video_pt(60,45-35,1); // East pt video_pt(60+37,45,1); video_pt(60+36,45,1); video_pt(60+35,45,1); // West pt video_pt(60-37,45,1); video_pt(60-36,45,1); video_pt(60-35,45,1); } delay++; switch (pointer) { case 1: if(counter >= 43) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 25; counter = 0; } counter++; break; case 2: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; case 3: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 4: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 5: if(counter >= 3) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 65; counter = 0; } counter++; break; case 6: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 7: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 8: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; } break; case 6: // West if (delay == 15) { // West pt video_pt(60-37,45,2); video_pt(60-36,45,2); video_pt(60-35,45,2); delay = 0; // South pt video_pt(60,45+37,1); video_pt(60,45+36,1); video_pt(60,45+35,1); // North pt video_pt(60,45-37,1); video_pt(60,45-36,1); video_pt(60,45-35,1); // East pt video_pt(60+37,45,1); video_pt(60+36,45,1); video_pt(60+35,45,1); } delay++; switch (pointer) { case 1: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 25; counter = 0; } counter++; break; case 2: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; case 3: if(counter >= 43) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 4: if(counter >= 33) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 5: if(counter >= 23) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 65; ounter = 0; } counter++; break; case 6: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 55; counter = 0; } counter++; break; case 7: if(counter >= 3) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 45; counter = 0; } counter++; break; case 8: if(counter >= 13) { TCCR0 = TCCR0 ^ 0b00011100; OCR0 = 35; counter = 0; } counter++; break; } break; } } else TCCR0 = 0; end //line 231 end //while end //main // ===================== END OF PROGRAM ======================