使用vim编辑编译c51程序
不准备考研了,所以决定学一下单片机什么的,不过今天终于受不了kei那差的要死的编辑器了,在网上发现了一个相当漂亮还能自动补全的软件sublime_text,功能也很强大,不过要想实现在这个软件中编译51程序好像还得学习一下它的插件制作教程,需要用到我感兴趣的python,不过资料太少,最终还是决定用vim,搞了一晚上终于把基本功能搞定了。
使用的vim版本:703
使用的Keil版本:Keil4
目前支持sbit,sfr等51 c特有关键字与uint,uchar的高亮,只可以编译单个的51c文件
以下 “~” 代表Vim所在目录
1,将keil\c51\bin加入到系统路径中,确保在命令行中可以使用c51.exe bl51.exe oh51.exe
2,打开~\vim73\filetype.vim 在大概200行左右插入:
au BufNewFile,BufRead *.c51 setf c51
3,在~\vim73\syntax目录下新建c51.vim,拷贝c.vim中的内容,在172行加入:
syn keyword cType sfr sfr16 bit sbit
syn keyword cType uchar uint
syn keyword cType uchar uint
4,在~\vim73\ftplugin目录下新建c51.vim:
" Vim filetype plugin file" Language: 51c" Maintainer: w-gh <wgh1992@gmail.com>" Last Change: 2013 Mar 21"Only do this when not done yet for this bufferif exists("b:did_ftplugin")finishendif" Don't load another plugin for this bufferlet b:did_ftplugin = 1" Using line continuation here.let s:cpo_save = &cposetlocal softtabstop=4"定义当前脚本有效的变量let s:lastShellReturn_C = 0 "最近一次编译的返回值let s:lastShellReturn_L = 0 "最近一次链接的返回值let s:ShowWarning = 1function Compiple()exe ":ccl""关闭快速修改窗口exe "update""文件如果有修改就进行保存let s:LastSheelReturn_C = 0let Sou = expand("%:p") "文档的完整路径let Obj = expand("%:p:r").s:Obj_Extension "obj文件的路径let v:statusmsg = '' "vim预定义变量"let Mp = &makeprgif !filereadable(Obj) || ((filereadable(Obj) && getftime(Obj) < getftime(Sou)))redraw! "更新屏幕setlocal makeprg=c51\ %"10sleepechohl WarningMsg | echo " compiling...."silent makeredraw!if v:shell_error != 0let s:LastSheelReturn_C = v:shell_errorendifif s:LastSheelReturn_C != 0exe ":bo cope"echohl WarningMsg | echo " compilation failed"elseif s:ShowWarningexe ":bo cw"endifechohl WarningMsg | echo " compilation successful"endifelse" echohl WarningMsg | echo "".Obj_Name."is up to date"endif"exe ":setlocal makeprg=Mpendfuncfunction Link()if s:LastSheelReturn_C != 0returnendiflet s:LastSheelReturn_L = 0"let Mp = &makeprglet v:statusmsg = ''setlocal makeprg=bl51\ %<.obj\ to\ %<.absechohl WarningMsg | echo " Creating Abs file...."silent makeredraw!if v:shell_error != 0let s:LastSheelReturn_L = v:shell_errorexe ":bo cope"echohl WarningMsg | echo "Creating Abs file failed"elseif s:ShowWarningexe ":bo cw"endifechohl WarningMsg | echo " Creating Abs file successful"endif"setlocal makeprg=Mpendfunctionfunction CreatHex()if s:LastSheelReturn_C != 0 || s:LastSheelReturn_L != 0returnendif"let Mp = &makeprglet v:statusmsg = ''setlocal makeprg=oh51\ %<.absechohl WarningMsg | echo " Creating Hex file...."silent makeredraw!if v:shell_error != 0exe ":bo cope"echohl WarningMsg | echo "Creating Hex file failed"elseif s:ShowWarningexe ":bo cw"endifechohl WarningMsg | echo " Creating Hex file successful"endif"setlocal makeprg=Mpendfunctionfunction Run()call Compiple()call Link()call CreatHex()endfunctionmap <F9> :call Run()<CR>let &cpo = s:cpo_saveunlet s:cpo_save
4,现在创建一个以.c51为后缀的文件,编辑完成后按F9键进行编译连接,并生成hex文件。
参考资料:
1,Vim.User.Manual

浙公网安备 33010602011771号