找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4569|回复: 1
打印 上一主题 下一主题
收起左侧

DIY信号发生器源码,AD格式原理图+PCB文件

[复制链接]
跳转到指定楼层
楼主
DIY信号发生器,原理图如下:


PCB文件



单片机源程序如下:
  1. //*****************************************************************************
  2. //
  3. // File Name        : 'main.c'
  4. // Title                : AVR DDS2 signal generator
  5. // Created                : 2008-03-09
  6. // Revised                : 2008-03-09
  7. // Version                : 2.0
  8. // Target MCU        : Atmel AVR series ATmega16
  9. //
  10. // This code is distributed under the GNU Public License
  11. //
  12. //*****************************************************************************
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <avr/io.h>
  16. #include <avr/pgmspace.h>
  17. #include <avr/eeprom.h>
  18. #include <avr/interrupt.h>
  19. #include <util/delay.h>
  20. #include <inttypes.h>
  21. #include "lcd_lib.h"
  22. //define R2R port
  23. #define R2RPORT PORTA
  24. #define R2RDDR DDRA
  25. //define button port and dedicated pins
  26. #define BPORT PORTD
  27. #define BPIN PIND
  28. #define BDDR DDRD
  29. #define DOWN 0//PORTD
  30. #define LEFT 1//PORTD
  31. #define START  2//PORTD
  32. #define RIGHT 3//PORTD
  33. #define UP 4//PORTD
  34. //Define Highs Speed (HS) signal output
  35. #define HSDDR DDRD
  36. #define HSPORT PORTD
  37. #define HS 5//PD5
  38. //define eeprom addresses
  39. #define EEMODE 0
  40. #define EEFREQ1 1
  41. #define EEFREQ2 2
  42. #define EEFREQ3 3
  43. #define EEDUTY 4
  44. #define EEINIT E2END
  45. #define RESOLUTION 0.095367431640625
  46. #define MINFREQ 0//minimum frequency
  47. #define MAXFREQ 65534//maximum DDS frequency
  48. #define MN_No 9// number of menu items

  49. //function prototypes
  50. void delay1s(void);
  51. void Timer2_Init(void);
  52. void Timer2_Start(void);
  53. void Timer2_Stop(void);
  54. void Main_Init(void);
  55. void Menu_Update(uint8_t);
  56. void Freq_Update(void);
  57. void Timer1_Start(uint8_t);
  58. void Timer1_Stop(void);
  59. void static inline Signal_OUT(const uint8_t *, uint8_t, uint8_t, uint8_t);
  60. //adjust LCDsendChar() function for strema
  61. static int LCDsendstream(char c, FILE *stream);
  62. //----set output stream to LCD-------
  63. static FILE lcd_str = FDEV_SETUP_STREAM(LCDsendstream, NULL, _FDEV_SETUP_WRITE);

  64. //Menu Strings in flash
  65. const uint8_t MN000[] PROGMEM="      Sine      \0";
  66. //menu 1
  67. const uint8_t MN100[] PROGMEM="     Square     \0";
  68. //menu 2
  69. const uint8_t MN200[] PROGMEM="    Triangle    \0";
  70. //menu 3
  71. const uint8_t MN300[] PROGMEM="    SawTooth    \0";
  72. //menu 4
  73. const uint8_t MN400[] PROGMEM="  Rev SawTooth  \0";
  74. //menu 5
  75. const uint8_t MN500[] PROGMEM="      ECG       \0";
  76. //menu 6
  77. const uint8_t MN600[] PROGMEM="    Freq Step   \0";
  78. //menu 7
  79. const uint8_t MN700[] PROGMEM="     Noise      \0";
  80. //menu 8
  81. const uint8_t MN800[] PROGMEM="   High Speed   \0";

  82. //Array of pointers to menu strings stored in flash
  83. const uint8_t *MENU[]={
  84.                 MN000,        //
  85.                 MN100,        //menu 1 string
  86.                 MN200,        //menu 2 string
  87.                 MN300,        //menu 3 string
  88.                 MN400,        //menu 4 string
  89.                 MN500,        
  90.                 MN600,
  91.                 MN700,
  92.                 MN800
  93.                 };
  94. //various LCD strings
  95. const uint8_t MNON[] PROGMEM="ON \0";//ON
  96. const uint8_t MNOFF[] PROGMEM="OFF\0";//OFF
  97. const uint8_t NA[] PROGMEM="       NA       \0";//Clear freq value
  98. const uint8_t CLR[] PROGMEM="                \0";//Clear freq value        
  99. const uint8_t MNClrfreq[] PROGMEM="           \0";//Clear freq value
  100. const uint8_t TOEEPROM[] PROGMEM="Saving Settings\0";//saving to eeprom
  101. const uint8_t ONEMHZ[] PROGMEM="      1MHz   \0";//saving to eeprom
  102. const uint8_t welcomeln1[] PROGMEM="AVR SIGNAL\0";
  103. const uint8_t RND[] PROGMEM="    Random\0";

  104. //variables to control TDA7313
  105. struct signal{
  106.         uint8_t mode;                //signal
  107.         uint8_t fr1;                //Frequency [0..7]
  108.         uint8_t fr2;                //Frequency [8..15]
  109.         uint8_t fr3;                //Frequency [16..31]
  110.         uint32_t freq;                //frequency value
  111.         uint8_t flag;                //if "0"generator is OFF, "1" - ON
  112.         uint32_t acc;                //accumulator
  113.         uint8_t ON;
  114.         uint8_t HSfreq;                //high speed frequency [1...4Mhz]
  115.         uint32_t deltafreq;        //frequency step value
  116. }SG;

  117. //define signals
  118. const uint8_t  sinewave[] __attribute__ ((section (".MySection1")))= //sine 256 values
  119. {
  120. 0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
  121. 0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
  122. 0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
  123. 0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
  124. 0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
  125. 0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
  126. 0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
  127. 0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
  128. 0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
  129. 0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
  130. 0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
  131. 0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
  132. 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
  133. 0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
  134. 0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
  135. 0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
  136. };
  137. const uint8_t squarewave[] __attribute__ ((section (".MySection2")))= //square wave
  138. {
  139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  141. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  142. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  143. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  144. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  145. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  146. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  147. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  148. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  149. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  150. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  151. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  152. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  153. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  154. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  155. };
  156. const uint8_t sawtoothwave[] __attribute__ ((section (".MySection3")))= //sawtooth wave
  157. {
  158. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  159. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  160. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
  161. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
  162. 0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
  163. 0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
  164. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  165. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
  166. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  167. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  168. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  169. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  170. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  171. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  172. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  173. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  174. };
  175. const uint8_t rewsawtoothwave[] __attribute__ ((section (".MySection4")))= //reverse sawtooth wave
  176. {
  177. 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0,
  178. 0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0,
  179. 0xdf,0xde,0xdd,0xdc,0xdb,0xda,0xd9,0xd8,0xd7,0xd6,0xd5,0xd4,0xd3,0xd2,0xd1,0xd0,
  180. 0xcf,0xce,0xcd,0xcc,0xcb,0xca,0xc9,0xc8,0xc7,0xc6,0xc5,0xc4,0xc3,0xc2,0xc1,0xc0,
  181. 0xbf,0xbe,0xbd,0xbc,0xbb,0xba,0xb9,0xb8,0xb7,0xb6,0xb5,0xb4,0xb3,0xb2,0xb1,0xb0,
  182. 0xaf,0xae,0xad,0xac,0xab,0xaa,0xa9,0xa8,0xa7,0xa6,0xa5,0xa4,0xa3,0xa2,0xa1,0xa0,
  183. 0x9f,0x9e,0x9d,0x9c,0x9b,0x9a,0x99,0x98,0x97,0x96,0x95,0x94,0x93,0x92,0x91,0x90,
  184. 0x8f,0x8e,0x8d,0x8c,0x8b,0x8a,0x89,0x88,0x87,0x86,0x85,0x84,0x83,0x82,0x81,0x80,
  185. 0x7f,0x7e,0x7d,0x7c,0x7b,0x7a,0x79,0x78,0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,
  186. 0x6f,0x6e,0x6d,0x6c,0x6b,0x6a,0x69,0x68,0x67,0x66,0x65,0x64,0x63,0x62,0x61,0x60,
  187. 0x5f,0x5e,0x5d,0x5c,0x5b,0x5a,0x59,0x58,0x57,0x56,0x55,0x54,0x53,0x52,0x51,0x50,
  188. 0x4f,0x4e,0x4d,0x4c,0x4b,0x4a,0x49,0x48,0x47,0x46,0x45,0x44,0x43,0x42,0x41,0x40,
  189. 0x3f,0x3e,0x3d,0x3c,0x3b,0x3a,0x39,0x38,0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x30,
  190. 0x2f,0x2e,0x2d,0x2c,0x2b,0x2a,0x29,0x28,0x27,0x26,0x25,0x24,0x23,0x22,0x21,0x20,
  191. 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,
  192. 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00,
  193. };

  194. const uint8_t trianglewave[] __attribute__ ((section (".MySection5")))= //triangle wave
  195. {
  196. 0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
  197. 0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
  198. 0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
  199. 0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
  200. 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
  201. 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
  202. 0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
  203. 0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
  204. 0xff,0xfd,0xfb,0xf9,0xf7,0xf5,0xf3,0xf1,0xef,0xef,0xeb,0xe9,0xe7,0xe5,0xe3,0xe1,
  205. 0xdf,0xdd,0xdb,0xd9,0xd7,0xd5,0xd3,0xd1,0xcf,0xcf,0xcb,0xc9,0xc7,0xc5,0xc3,0xc1,
  206. 0xbf,0xbd,0xbb,0xb9,0xb7,0xb5,0xb3,0xb1,0xaf,0xaf,0xab,0xa9,0xa7,0xa5,0xa3,0xa1,
  207. 0x9f,0x9d,0x9b,0x99,0x97,0x95,0x93,0x91,0x8f,0x8f,0x8b,0x89,0x87,0x85,0x83,0x81,
  208. 0x7f,0x7d,0x7b,0x79,0x77,0x75,0x73,0x71,0x6f,0x6f,0x6b,0x69,0x67,0x65,0x63,0x61,
  209. 0x5f,0x5d,0x5b,0x59,0x57,0x55,0x53,0x51,0x4f,0x4f,0x4b,0x49,0x47,0x45,0x43,0x41,
  210. 0x3f,0x3d,0x3b,0x39,0x37,0x35,0x33,0x31,0x2f,0x2f,0x2b,0x29,0x27,0x25,0x23,0x21,
  211. 0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x11,0x0f,0x0f,0x0b,0x09,0x07,0x05,0x03,0x01
  212. };
  213. const uint8_t ECG[] __attribute__ ((section (".MySection6")))= //ECG wave
  214. {
  215. 73,74,75,75,74,73,73,73,73,72,71,69,68,67,67,67,
  216. 68,68,67,65,62,61,59,57,56,55,55,54,54,54,55,55,
  217. 55,55,55,55,54,53,51,50,49,49,52,61,77,101,132,
  218. 169,207,238,255,254,234,198,154,109,68,37,17,5,
  219. 0,1,6,13,20,28,36,45,52,57,61,64,65,66,67,68,68,
  220. 69,70,71,71,71,71,71,71,71,71,72,72,72,73,73,74,
  221. 75,75,76,77,78,79,80,81,82,83,84,86,88,91,93,96,
  222. 98,100,102,104,107,109,112,115,118,121,123,125,
  223. 126,127,127,127,127,127,126,125,124,121,119,116,
  224. 113,109,105,102,98,95,92,89,87,84,81,79,77,76,75,
  225. 74,73,72,70,69,68,67,67,67,68,68,68,69,69,69,69,
  226. 69,69,69,70,71,72,73,73,74,74,75,75,75,75,75,75,
  227. 74,74,73,73,73,73,72,72,72,71,71,71,71,71,71,71,
  228. 70,70,70,69,69,69,69,69,70,70,70,69,68,68,67,67,
  229. 67,67,66,66,66,65,65,65,65,65,65,65,65,64,64,63,
  230. 63,64,64,65,65,65,65,65,65,65,64,64,64,64,64,64,
  231. 64,64,65,65,65,66,67,68,69,71,72,73
  232. };
  233. //array of pointers to signal tables
  234. const uint8_t *SIGNALS[] ={
  235.         sinewave,
  236.         squarewave,
  237.         trianglewave,
  238.         sawtoothwave,
  239.         rewsawtoothwave,
  240.         ECG
  241. };
  242. //adjust LCD stream fuinction to use with printf()
  243. static int LCDsendstream(char c , FILE *stream)
  244. {
  245. LCDsendChar(c);
  246. return 0;
  247. }
  248. //delay 1s
  249. void delay1s(void)
  250. {
  251.         uint8_t i;
  252.         for(i=0;i<100;i++)
  253.         {
  254.                 _delay_ms(10);
  255.         }
  256. }
  257. //initialize Timer2 (used for button reading)
  258. void Timer2_Init(void)
  259. {
  260.         TCNT2=0x00;
  261.         sei();
  262. }
  263. //start timer2
  264. void Timer2_Start(void)
  265. {
  266.         TCCR2|=(1<<CS22)|(1<<CS21); //prescaller 256 ~122 interrupts/s
  267.         TIMSK|=(1<<TOV2);//Enable Timer0 Overflow interrupts
  268. }
  269. //stop timer 2
  270. void Timer2_Stop(void)
  271. {
  272.         TCCR0&=~((1<<CS22)|(1<<CS21)); //Stop timer0
  273.         TIMSK&=~(1<<TOV2);//Disable Timer0 Overflow interrupts

  274. }

  275. //Initial menu
  276. //show initial signal and frequency
  277. //generator is off
  278. void Menu_Update(uint8_t on)
  279. {
  280.         LCDclr();
  281.         CopyStringtoLCD(MENU[(SG.mode)], 0, 0 );
  282.         LCDGotoXY(0, 1);
  283.         if (SG.mode==6)
  284.                 {
  285.                         CopyStringtoLCD(CLR, 0, 1 );
  286.                         LCDGotoXY(0, 1);
  287.                         printf("    %5uHz", (uint16_t)SG.deltafreq);
  288.                 }
  289.         if (SG.mode==7)
  290.                 {
  291.                         CopyStringtoLCD(CLR, 0, 1 );
  292.                         CopyStringtoLCD(RND, 0, 1 );
  293.                 }
  294.         if (SG.mode==8)
  295.                 {
  296.                 CopyStringtoLCD(CLR, 0, 1 );
  297.                 LCDGotoXY(0, 1);
  298.                 printf(" %5uMHz", SG.HSfreq);
  299.                 }
  300.         if((SG.mode==0)||(SG.mode==1)||(SG.mode==2)||(SG.mode==3)||(SG.mode==4)||(SG.mode==5))
  301.                 {
  302.                         CopyStringtoLCD(CLR, 0, 1 );
  303.                         LCDGotoXY(0, 1);
  304.                         printf(" %5uHz", (uint16_t)SG.freq);
  305.                 }
  306.         if (SG.mode!=6)
  307.         {
  308.                 if(on==1)
  309.                         CopyStringtoLCD(MNON, 13, 1 );
  310.                 else
  311.                         CopyStringtoLCD(MNOFF, 13, 1 );
  312.         }
  313. }
  314. //update frequency value on LCD menu - more smooth display
  315. void Freq_Update(void)
  316. {
  317. if (SG.mode==6)
  318. {
  319.         LCDGotoXY(0, 1);
  320.         printf("    %5uHz", (uint16_t)SG.deltafreq);
  321. }
  322. if (SG.mode==8)
  323. {
  324. //if HS signal
  325.         LCDGotoXY(0, 1);
  326.         printf(" %5uMHz", SG.HSfreq);
  327. }
  328. if((SG.mode==0)||(SG.mode==1)||(SG.mode==2)||(SG.mode==3)||(SG.mode==4)||(SG.mode==5))
  329.         {
  330.                 LCDGotoXY(0, 1);
  331.                 printf(" %5uHz", (uint16_t)SG.freq);
  332.         }
  333. }

  334. //External interrupt0 service routine
  335. //used to stop DDS depending on active menu
  336. //any generator is stopped by setting flag value to 0
  337. //DDs generator which is inline ASM is stopped by setting
  338. //CPHA bit in SPCR register
  339. ISR(INT0_vect)
  340. {
  341. SG.flag=0;//set flag to stop generator
  342. SPCR|=(1<<CPHA);//using CPHA bit as stop mark
  343. //CopyStringtoLCD(MNOFF, 13, 1 );
  344. SG.ON=0;//set off in LCD menu
  345. loop_until_bit_is_set(BPIN, START);//wait for button release
  346. }
  347. //timer overflow interrupt service tourine
  348. //checks all button status and if button is pressed
  349. //value is updated
  350. ISR(TIMER2_OVF_vect)
  351. {
  352. if (bit_is_clear(BPIN, UP))
  353. //Button UP increments value which selects previous signal mode
  354. //if first mode is reached - jumps to last
  355.         {
  356.         if (SG.mode==0)
  357.         {
  358.                 SG.mode=MN_No-1;
  359.         }
  360.         else
  361.         {
  362.                 SG.mode--;
  363.         }
  364.         //Display menu item
  365.         Menu_Update(SG.ON);
  366.         loop_until_bit_is_set(BPIN, UP);
  367.         }
  368. if (bit_is_clear(BPIN, DOWN))
  369. //Button Down decrements value which selects next signal mode
  370. //if last mode is reached - jumps to first
  371.         {
  372.         if (SG.mode<(MN_No-1))
  373.                         {
  374.                                 SG.mode++;
  375.                         }
  376.                 else
  377.                         {
  378.                                 SG.mode=0;
  379.                         }
  380.         //Display menu item
  381.         Menu_Update(SG.ON);
  382.         loop_until_bit_is_set(BPIN, DOWN);
  383.         }
  384. if (bit_is_clear(BPIN, RIGHT))
  385. //frequency increment
  386.         {
  387.                 if(SG.mode==6)
  388.                 {
  389.                         if(SG.deltafreq==10000)
  390.                                 SG.deltafreq=1;
  391.                         else
  392.                                 SG.deltafreq=(SG.deltafreq*10);
  393.                         Freq_Update();
  394.                         loop_until_bit_is_set(BPIN, RIGHT);
  395.                 }
  396.                 if (SG.mode==8)
  397.                         {
  398.                         //ifhigh speed signal
  399.                         if(SG.HSfreq==8)
  400.                                 SG.HSfreq=1;
  401.                         else
  402.                                 SG.HSfreq=(SG.HSfreq<<1);
  403.                         Freq_Update();
  404.                         loop_until_bit_is_set(BPIN, RIGHT);
  405.                         }
  406.                 if((SG.mode==0)||(SG.mode==1)||(SG.mode==2)||(SG.mode==3)||(SG.mode==4)||(SG.mode==5))
  407.                         {
  408.                                 if ((0xFFFF-SG.freq)>=SG.deltafreq)
  409.                                         SG.freq+=SG.deltafreq;
  410.                                 Freq_Update();
  411.                                 uint8_t ii=0;
  412.                                 //press button and wait for long press (~0.5s)
  413.                                 do{
  414.                                         _delay_ms(2);
  415.                                         ii++;
  416.                                 }while((bit_is_clear(BPIN, RIGHT))&&(ii<=250));//wait for button release
  417.                                 if(ii>=250)
  418.                                 {
  419.                                         do{
  420.                                                 if ((0xFFFF-SG.freq)>=SG.deltafreq)
  421.                                                         SG.freq+=SG.deltafreq;
  422.                                                 Freq_Update();
  423.                                         }while(bit_is_clear(BPIN, RIGHT));//wait for button release
  424.                                 }
  425.                         }
  426.         }
  427. if (bit_is_clear(BPIN, LEFT))
  428. //frequency decrement
  429.         {
  430.                 if(SG.mode==6)
  431.                 {
  432.                         if(SG.deltafreq==1)
  433.                                 SG.deltafreq=10000;
  434.                         else
  435.                                 SG.deltafreq=(SG.deltafreq/10);
  436.                         Freq_Update();
  437.                         loop_until_bit_is_set(BPIN, LEFT);
  438.                 }
  439.                 if (SG.mode==8)
  440.                         {
  441.                         //ifhigh speed signal
  442.                         if(SG.HSfreq==1)
  443.                                 SG.HSfreq=8;
  444.                         else
  445.                                 SG.HSfreq=(SG.HSfreq>>1);
  446.                         Freq_Update();
  447.                         loop_until_bit_is_set(BPIN, LEFT);
  448.                         }
  449.                 if ((SG.mode==0)||(SG.mode==1)||(SG.mode==2)||(SG.mode==3)||(SG.mode==4)||(SG.mode==5))
  450.                         {
  451.                                 if (SG.freq>=SG.deltafreq)
  452.                                         SG.freq-=SG.deltafreq;
  453.                                 Freq_Update();
  454.                                 uint8_t ii=0;
  455.                                 //press button and wait for long press (~0.5s)
  456.                                 do{
  457.                                         _delay_ms(2);
  458.                                         ii++;
  459.                                 }while((bit_is_clear(BPIN, LEFT))&&(ii<=250));//wait for button release
  460.                                 if(ii>=250)
  461.                                 {
  462.                                         do{
  463.                                                 if (SG.freq>=SG.deltafreq)
  464.                                                         SG.freq-=SG.deltafreq;
  465.                                                 Freq_Update();
  466.                                         }while(bit_is_clear(BPIN, LEFT));//wait for button release
  467.                                 }
  468.                         }
  469.         }
  470. if (bit_is_clear(BPIN, START))
  471.         {
  472. if(SG.mode!=6)
  473.         {
  474.                 //saving last configuration
  475.                 SG.fr1=(uint8_t)(SG.freq);
  476.                 SG.fr2=(uint8_t)(SG.freq>>8);
  477.                 SG.fr3=(uint8_t)(SG.freq>>16);
  478.                 if (eeprom_read_byte((uint8_t*)EEMODE)!=SG.mode) eeprom_write_byte((uint8_t*)EEMODE,SG.mode);
  479.                 if (eeprom_read_byte((uint8_t*)EEFREQ1)!=SG.fr1) eeprom_write_byte((uint8_t*)EEFREQ1,SG.fr1);
  480.                 if (eeprom_read_byte((uint8_t*)EEFREQ2)!=SG.fr2) eeprom_write_byte((uint8_t*)EEFREQ2,SG.fr2);
  481.                 if (eeprom_read_byte((uint8_t*)EEFREQ3)!=SG.fr3) eeprom_write_byte((uint8_t*)EEFREQ3,SG.fr3);
  482.                 //Calculate frequency value from restored EEPROM values
  483.                 SG.freq=(((uint32_t)(SG.fr3)<<16)|((uint32_t)(SG.fr2)<<8)|((uint32_t)(SG.fr1)));
  484.                 //calculate accumulator value
  485.                 SG.acc=SG.freq/RESOLUTION;
  486.                 SG.flag=1;//set flag to start generator
  487.                 SG.ON=1;//set ON on LCD menu
  488.                 SPCR&=~(1<<CPHA);//clear CPHA bit in SPCR register to allow DDS
  489.                 //Stop timer2 - menu inactive
  490.                 Timer2_Stop();
  491.                 //display ON on LCD
  492.                 Menu_Update(SG.ON);
  493.         }
  494.         loop_until_bit_is_set(BPIN, START);//wait for button release
  495.         }
  496. }
  497. /*DDS signal generation function
  498. Original idea is taken from
  499. http://www.myplace.nu/avr/minidds/index.htm
  500. small modification is made - added additional command which
  501. checks if CPHA bit is set in SPCR register if yes - exit function
  502. */
  503. void static inline Signal_OUT(const uint8_t *signal, uint8_t ad2, uint8_t ad1, uint8_t ad0)
  504. {
  505. asm volatile(        "eor r18, r18         ;r18<-0"        "\n\t"
  506.                                 "eor r19, r19         ;r19<-0"        "\n\t"
  507.                                 "1:"                                                "\n\t"
  508.                                 "add r18, %0        ;1 cycle"                        "\n\t"
  509.                                 "adc r19, %1        ;1 cycle"                        "\n\t"        
  510.                                 "adc %A3, %2        ;1 cycle"                        "\n\t"
  511.                                 "lpm                         ;3 cycles"         "\n\t"
  512.                                 "out %4, __tmp_reg__        ;1 cycle"        "\n\t"
  513.                                 "sbis %5, 2                ;1 cycle if no skip" "\n\t"
  514.                                 "rjmp 1b                ;2 cycles. Total 10 cycles"        "\n\t"
  515.                                 :
  516.                                 :"r" (ad0),"r" (ad1),"r" (ad2),"e" (signal),"I" (_SFR_IO_ADDR(PORTA)), "I" (_SFR_IO_ADDR(SPCR))
  517.                                 :"r18", "r19"
  518.         );
  519. }

  520. void Timer1_Start(uint8_t FMHz)
  521. {
  522. switch(FMHz){
  523.         case 1:
  524.                 //start high speed (1MHz) signal
  525.                 OCR1A=7;
  526.                 break;
  527.         case 2:
  528.                 OCR1A=3;//2MHz
  529.                 break;
  530.         case 4:
  531.                 OCR1A=1;//4MHz
  532.                 break;
  533.         case 8:
  534.                 OCR1A=0;//8MHz
  535.                 break;
  536.         default:
  537.                 OCR1A=7;//defauls 1MHz
  538.                 break;}
  539.         //Output compare toggles OC1A pin
  540.         TCCR1A=0x40;
  541.         //start timer without prescaler
  542.         TCCR1B=0b00001001;
  543. }
  544. void Timer1_Stop(void)
  545. {
  546.         TCCR1B=0x00;//timer off
  547. }
  548. //main init function
  549. void Main_Init(void)
  550. {
  551. //stderr = &lcd_str;
  552. stdout = &lcd_str;
  553. //--------init LCD----------
  554. LCDinit();
  555. LCDclr();
  556. LCDcursorOFF();
  557. //-------EEPROM initial values----------
  558. if (eeprom_read_byte((uint8_t*)EEINIT)!='T')
  559. {
  560. eeprom_write_byte((uint8_t*)EEMODE,0x00);//initial mode 0 ?OUT~~~~;
  561. eeprom_write_byte((uint8_t*)EEFREQ1,0xE8);//initial frequency 1kHz
  562. eeprom_write_byte((uint8_t*)EEFREQ2,0x03);
  563. eeprom_write_byte((uint8_t*)EEFREQ3,0x00);
  564. eeprom_write_byte((uint8_t*)EEINIT,'T');//marks once that eeprom init is done
  565. //once this procedure is held, no more initialization is performed
  566. }
  567. //------restore last saved values from EEPROM------
  568. SG.mode=eeprom_read_byte((uint8_t*)EEMODE);
  569. SG.fr1=eeprom_read_byte((uint8_t*)EEFREQ1);
  570. SG.fr2=eeprom_read_byte((uint8_t*)EEFREQ2);
  571. SG.fr3=eeprom_read_byte((uint8_t*)EEFREQ3);
  572. SG.freq=(((uint32_t)(SG.fr3)<<16)|((uint32_t)(SG.fr2)<<8)|((uint32_t)(SG.fr1)));
  573. SG.acc=SG.freq/RESOLUTION;
  574. SG.flag=0;
  575. //default 1MHz HS signal freq
  576. SG.HSfreq=1;
  577. SG.deltafreq=100;
  578. //------------init DDS output-----------
  579. R2RPORT=0x00;//set initial zero values
  580. R2RDDR=0xFF;//set A port as output
  581. //-------------set ports pins for buttons----------
  582. BDDR&=~(_BV(START)|_BV(UP)|_BV(DOWN)|_BV(RIGHT)|_BV(LEFT));
  583. BPORT|=(_BV(START)|_BV(UP)|_BV(DOWN)|_BV(RIGHT)|_BV(LEFT));
  584. //---------set ports pins for HS output---------
  585. HSDDR|=_BV(HS);//configure as output
  586. //-----------Menu init--------------
  587. SG.ON=0;//default signal is off
  588. Menu_Update(SG.ON);
  589. //-----------Timer Init-------------
  590. Timer2_Init();
  591. //Start Timer with overflow interrupts
  592. Timer2_Start();
  593. }

  594. int main(void)
  595. {
  596. //Initialize
  597. Main_Init();
  598. while(1)//infinite loop
  599.         {
  600.         if (SG.flag==1)
  601.                 {
  602. ……………………

  603. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
程序与PCB原理图.rar (157.11 KB, 下载次数: 29)



评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:96218 发表于 2017-10-25 08:52 | 只看该作者
这个已经严重过没有问题。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表